lexical_cast and default params
Running into something very strange with boost::lexical_cast in 1.39.
This code works, but notice the f(). If I attempt to get rid of it and
call boost::lexical_cast directly in the constructor for test I get this
error:
1>d:\dev_workspace\experimental\scratch\scratch\main.cpp(16) : error
C2783: 'Target boost::lexical_cast(const Source &)' : could not deduce
template argument for 'Target'
1> d:\boostvs39\include\boost\lexical_cast.hpp(1164) : see
declaration of 'boost::lexical_cast'
This is hard for me to understand since I've given the target type as a
template parameter....and the f() version works just fine. It even
works if I make it inline as the lexical_cast function is.
Any ideas?
#include
In article
Running into something very strange with boost::lexical_cast in 1.39.
This code works, but notice the f().
Interestingly, when actually applied to the production code I'm having trouble with this method did not work. I got the same error for my wrapper and had to set it up to return std::string only. In my production code I put the wrapper in a detail_ namespace for what I was working on. Turns out that scope resolution at the call site causes the problem. Tested this fact by placing a "using namespace boost" above the test code and replacing f() with lexical_cast(); it worked fine. This seems like a compiler bug to me unless someone can tell me why a template parameter supplied at the call site would be unresolved only when you're using the scope operator to find it. AFAIK a supplied parameter should never be "unresolved" and I can't see why this would be a special case. Leaving the original code pasted below for reference.
#include
#include <string> #include <iostream>
// if f() is in a nampace and we use scope resolution in test // test constructor, this will fail to resolve the R supplied at // the call site. MSVC++ 8.0
template < typename R, typename T > R f(T const& x) { return boost::lexical_cast<R>(x); }
template < typename T > struct test { test(std::string const& x = fstd::string(T())) : test_string(x) {}
std::string test_string; };
int main() { test<int> t;
std::cout << t.test_string; std::cout << boost::lexical_caststd::string(int());
std::cin.get(); }
participants (1)
-
Noah Roberts