function overloading. const T and const T* parameters

This is a discussion on function overloading. const T and const T* parameters within the C++ Programming forums, part of the General Programming Boards category; A compile-time error, apparently to support compatibility with C. Code: void foo (int); void foo (const int); Now... I did ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578

    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    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
    22,269
    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.""
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Portugal
    Posts
    7,578
    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;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    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
    Portugal
    Posts
    7,578
    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...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,399
    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
    Portugal
    Posts
    7,578
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21