N
Noah Roberts
What is wrong with the following code because I just can't see it:
#include <string>
#include <iostream>
namespace something {
template < typename R, typename T >
inline R f(T const& x)
{
return R(x);
}
}
template < typename T >
struct test
{
test(double d = something::f<double>(5))
: test_double(d) {}
double test_double;
};
int main()
{
test<int> t;
std::cout << t.test_double;
std::cin.get();
}
The compiler complains that it cannot resolve R in the call to
something::f<double>() in the test constructor. It only happens in
default arguments and only if you need namespace resolution to get to f
<>.
If the line "test(double d = something::f<double>(5))" is changed to
"test(double d = f<double>(5))" and a "using namespace something" is
placed above the structure declaration, the code compiles just fine.
Here's the actual compiler error:
1>d:\dev_workspace\experimental\scratch\scratch\main.cpp(17) : error
C2783: 'R something::f(const T &)' : could not deduce template argument
for 'R'
1> d:\dev_workspace\experimental\scratch\scratch\main.cpp(7) :
see declaration of 'something::f'
This makes no sense to me at all. Am I running into some obscure C++
standard thing or is this compiler just broken?
This was actually discovered in an attempt to provide a string default
argument through a boost::lexical_cast.
#include <string>
#include <iostream>
namespace something {
template < typename R, typename T >
inline R f(T const& x)
{
return R(x);
}
}
template < typename T >
struct test
{
test(double d = something::f<double>(5))
: test_double(d) {}
double test_double;
};
int main()
{
test<int> t;
std::cout << t.test_double;
std::cin.get();
}
The compiler complains that it cannot resolve R in the call to
something::f<double>() in the test constructor. It only happens in
default arguments and only if you need namespace resolution to get to f
<>.
If the line "test(double d = something::f<double>(5))" is changed to
"test(double d = f<double>(5))" and a "using namespace something" is
placed above the structure declaration, the code compiles just fine.
Here's the actual compiler error:
1>d:\dev_workspace\experimental\scratch\scratch\main.cpp(17) : error
C2783: 'R something::f(const T &)' : could not deduce template argument
for 'R'
1> d:\dev_workspace\experimental\scratch\scratch\main.cpp(7) :
see declaration of 'something::f'
This makes no sense to me at all. Am I running into some obscure C++
standard thing or is this compiler just broken?
This was actually discovered in an attempt to provide a string default
argument through a boost::lexical_cast.