R
Rein Anders Apeland
Consider the following working code:
#include <memory>
#include <iostream>
void print_ptr( const std::auto_ptr< int > & thePtr =
std::auto_ptr< int >() )
{
std::cout << "Ptr address: " << thePtr.get() << std::endl;
}
int main()
{
std::auto_ptr< int > somePtr( new int );
print_ptr( somePtr );
print_ptr();
return 0;
}
Using GCC 3.3.4 it compiles without warning.
Using GCC 4.0.2 it gives the following errors:
foo.cpp: In function ‘int main()’:
foo.cpp:15: error: no matching function for call to
‘std::auto_ptr<int>::auto_ptr(std::auto_ptr<int>)’
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../include/c
++/4.0.2/memory:349: note: candidates are:
std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = int]
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../include/c
++/4.0.2/memory:212: note:
std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&) [with _Tp1 = int, _Tp
= int]
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../include/c
++/4.0.2/memory:199: note:
std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = int]
foo.cpp:6: error: in passing argument 1 of ‘void print_ptr(const
std::auto_ptr<int>&)’
It appears that having a default value of 'std::auto_ptr<T>()' to
a parameter of type 'const std::auto_ptr<T> &' is not allowed in
GCC 4.0.2. Is there something in the C++ standard that is enforced in
4.0.2 and not in 3.3.4, or is it purely compiler-specific.
Does it have anything to do with the parameter being a reference?
#include <memory>
#include <iostream>
void print_ptr( const std::auto_ptr< int > & thePtr =
std::auto_ptr< int >() )
{
std::cout << "Ptr address: " << thePtr.get() << std::endl;
}
int main()
{
std::auto_ptr< int > somePtr( new int );
print_ptr( somePtr );
print_ptr();
return 0;
}
Using GCC 3.3.4 it compiles without warning.
Using GCC 4.0.2 it gives the following errors:
foo.cpp: In function ‘int main()’:
foo.cpp:15: error: no matching function for call to
‘std::auto_ptr<int>::auto_ptr(std::auto_ptr<int>)’
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../include/c
++/4.0.2/memory:349: note: candidates are:
std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = int]
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../include/c
++/4.0.2/memory:212: note:
std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&) [with _Tp1 = int, _Tp
= int]
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../include/c
++/4.0.2/memory:199: note:
std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = int]
foo.cpp:6: error: in passing argument 1 of ‘void print_ptr(const
std::auto_ptr<int>&)’
It appears that having a default value of 'std::auto_ptr<T>()' to
a parameter of type 'const std::auto_ptr<T> &' is not allowed in
GCC 4.0.2. Is there something in the C++ standard that is enforced in
4.0.2 and not in 3.3.4, or is it purely compiler-specific.
Does it have anything to do with the parameter being a reference?