Boost logo

Boost Users :

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


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 <boost/lexical_cast.hpp>
#include <string>
#include <iostream>

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