Thread: function overloading. const T and const T* parameters

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    function overloading. const T and const T* parameters

    A compile-time error, apparently to support compatibility with C.

    Code:
    void foo (int);
    void foo (const int);
    Now... I did this with pointers like so,

    Code:
    void foo (int*);
    void foo (const int*);
    ...and it compiled just fine. My question is, why? I would like to understand this past the simple notion that function overloading makes no distinction between const types unless they are pointers to const.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    The pointer version compiled fine? Are you sure? Did you try calling it? This doesn't compile for me.

    Code:
    #include <cstdio>
    void foo (int* ho ){}
    void foo (const int* ho ) {}
    int main() {
      foo(0); 
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The pointer version compiled fine? Are you sure? Did you try calling it? This doesn't compile for me.
    Perhaps try:
    Code:
    void foo (int* ho ) {}
    void foo (const int* ho ) {}
    int main() {
    	int x = 1;
    	const int y = 2;
    	foo(&x);
    	foo(&y);
    }
    Since 0 is convertible to any pointer type, my guess is that it made the call to foo ambiguous.

    According to section 13.1 of the C++ Standard:
    "Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations. In particular, for any type T, "pointer to T," "pointer to const T," and "pointer to volatile T" are considered distinct parameter types, as are "reference to T," "reference to const T," and "reference to volatile T.""
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Both on Dev-C++ and VC++ 2005 Express.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    void foo (int *bar) {
        std::cout << "inside void foo (int *bar)   : " << *bar << std::endl;
    }
    
    void foo (const int *bar) {
        std::cout << "inside void foo (const int *bar)   : " << *bar << std::endl;
    }
    
    int main()
    {
        using namespace std;
    
        int x = 2;
        int *p = &x;
        const int *k = &x;
    
        foo(p); foo(k);
    
        system("PAUSE");
    	return 0;
    }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Quote Originally Posted by laserlight
    According to section 13.1 of the C++ Standard
    Interesting.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by laserlight
    According to section 13.1 of the C++ Standard[...]
    Ah! Thanks laserlight.
    Need to learn to search the ISO more effectively...
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Mario F.
    Need to learn to search the ISO more effectively...
    I dunno, I think you demonstrated a pretty effective manner of searching: let someone else do it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I have a PDF copy of the 14882. Since C++ is a new programming language to me, not always I know exactly where to search. It's not exactly an easy document for a newcomer. Also, I couldn't come with an effective search string on google.

    Rest assured... The doubts I haven't post because I found the answer by far surpass the ones I had to ask here.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed