Question

I have this code example:

#include <iostream>
#include <memory>

template <typename T>
void func1(T& value)
{
    std::cout << "passed 1 ..." << std::endl;
}

template <template <typename> class T, typename U>
void func2(T<U>& value)
{
    std::cout << "passed 2 ..." << std::endl;
}

int main()
{
    std::auto_ptr<int> a;
    const std::auto_ptr<int> ca;

    // case 1: using func1
    func1(a);  // OK
    func1(ca); // OK

    // case 2: using func2
    func2(a);  // OK
    func2(ca); // Compilation error

    return 0;
}

In the first case the function 'func1' accepts a generic argument regardless of the qualifier, however the second case the function 'func2' fails when the argument has the const qualifier. Why this happens ?

This is the compilation error:

make all 
Building file: ../src/Test2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Test2.d" -MT"src/Test2.d" -o "src/Test2.o" "../src/Test2.cpp"
../src/Test2.cpp: In function ‘int main()’:
../src/Test2.cpp:27: error: invalid initialization of reference of type ‘std::auto_ptr<int>&’ from expression of type ‘const std::auto_ptr<int>’
../src/Test2.cpp:11: error: in passing argument 1 of ‘void func2(T<U>&) [with T = std::auto_ptr, U = int]’
make: *** [src/Test2.o] Error 1
Was it helpful?

Solution

The problem is that in the case of func1, the compiler needs to deduce T and we get

  • T is std::auto_ptr<int> in the first call
  • T is const std::auto_ptr<int> in the second call

In both cases T is valid in itself.

Now for func2, the compiler needs to deduce T and U, where T is a template template parameter. What would be needed is:

  • T is std::auto_ptr, U is int in the first call
  • T is const std::auto_ptr, U is int in the second call

and there's your problem: T can not be const std::auto_ptr itself, as it combined a type-property const with a template std::auto_ptr which is not a valid type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top