Should type deduction remove CVs from Arrays?

Should type deduction remove CVs from Arrays?

Post by Richard Corde » Sun, 03 Aug 2003 17:54:52

There are different results with some compilers when deducing a
template parameter 'T const &', from an argument of const array type.

For the following example, g++ (2.95.3, 3.2.2) deduces T to be 'char
[10]' and Comeau deduces T to be 'const char [10]'.

void bar (char *);
void bar (int &);

template <typename T>
void foo (T const &p)
   T t = {0};
   bar (t); // Comeau generates an error here, T=const char []


void bar ()
   int j;
   int const & i = j;
   foo (i); // T deduced as int

   const char ca[10] = {0};
   foo (ca); // T deduced as?



My reading of the standard leads me to the conclusion that g++ is
correct, at least it is consistent with the case of const int.

This could be important where template programming chooses
specializations/overloads based on constness.



Richard Corden
To reply remove 's' from address

      [ See for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


1. cv overloading and template argument deduction

Check out this small example:

#include <functional>
#include <iostream>

struct A
    int f() const {std::cout << "const f\n";}
    int f() {std::cout << "non-const f\n";}

int main()
    A a;
    std::mem_fun(&A::f) (&a);

It compiles without warning on gcc 2.95 with stlport, and produces the
const f

mem_fun is a template function with two overloads - one for constant
member functions. The constant overload is chosen by gcc, and I have
no idea why.

Is this correct? I would have thought that the call to mem_fun would
be ambiguous since the one returning a const_mem_fun_t and the one
returning a mem_fun_t seem to both be possibles, and overload
resolution should be unable to choose between them, shouldn't it?
Certainly if you replace "int f() const" with "int f(int i)" then gcc
gives the error I would expect.

I delved into the CD2 standard without success. 14.8.2, 13.3 and 13.4
all seem relevent, but I was unable to put it all together.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ:              ]

2. emm386, 512MB ram, asus p4pe

3. Conversion function templates: why remove CV, & ?

4. Running VSG demo in normal matlab via VSG board?

5. template function argument deduction for constraints on allowable types

6. Which PDM for PIX 6.3.1

7. Functors and template argument type deduction

8. MPC50X Power Micro users?

9. Automatic template parameter type deduction on constructors

10. Function template deduction from return types

11. Function template argument type deduction

12. Template type deduction query

13. Matching template argument types with cv-qualifiers