Boost logo

Boost Users :

Subject: Re: [Boost-users] lexical_cast and default params
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2009-12-01 13:55:45


In article <MPG.257f02b13a41ac01989684_at_[hidden]>,
roberts.noah_at_[hidden] says...
>
> 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 <boost/lexical_cast.hpp>
> #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 = f<std::string>(T()))
> : test_string(x) {}
>
> std::string test_string;
> };
>
> int main()
> {
> test<int> t;
>
> std::cout << t.test_string;
> std::cout << boost::lexical_cast<std::string>(int());
>
> std::cin.get();
> }


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net