4

I am having problems compiling the following snippet

int temp; 
vector<int> origins;

vector<string> originTokens = OTUtils::tokenize(buffer, ","); // buffer is a char[] array

// original loop 
BOOST_FOREACH(string s, originTokens)
{
    from_string(temp, s);
    origins.push_back(temp);
}    

// I'd like to use this to replace the above loop
std::transform(originTokens.begin(), originTokens.end(), origins.begin(), boost::bind<int>(&FromString<int>, boost::ref(temp), _1));

where the function in question is

// the third parameter should be one of std::hex, std::dec or std::oct
template <class T>
bool FromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&) = std::dec)
{
    std::istringstream iss(s);
    return !(iss >> f >> t).fail();
}

the error I get is

1>Compiling with Intel(R) C++ 11.0.074 [IA-32]... (Intel C++ Environment)
1>C:\projects\svn\bdk\Source\deps\boost_1_42_0\boost/bind/bind.hpp(303): internal error: assertion failed: copy_default_arg_expr: rout NULL, no error (shared/edgcpfe/il.c, line 13919)
1>
1>          return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
1>                                                                                ^
1>
1>icl: error #10298: problem during post processing of parallel object compilation

Google is being unusually unhelpful so I hope that some one here can provide some insights.

UPDATE: Question is answered by @gf's solution, but interestingly the original function wasn't quite correct as it stored the result of the conversion operation (failed/not failed) rather than the converted value itself. I changed the signature return the converted value directly and the compiler was able to infer the type of the binding correctly.

// new signature
T FromString(const std::string& s, std::ios_base& (*f)(std::ios_base&) = std::dec);

// revised calling syntax - note that bind<int> is not required. 
transform(originTokens.begin(), originTokens.end(), origins.begin(),  bind(&FromString<int>, _1, std::dec));
4

2 回答 2

4

FromString() takes 3 arguments and default arguments are not part of a functions type. So the bind expression should probably be something like:

boost::bind<int>(&FromString<int>, boost::ref(temp), _1, std::dec);
于 2010-04-26T09:59:16.727 回答
0

Function templates aren't supported by boost::bind. You should turn the function into a function object:

template< typename T> 
struct from_string {
  bool operator()(T& t, std::string const& s, std::ios_base& (*f)(std::ios_base&) = std::dec) {
    std::istringstream iss(s);
    return !(iss >> f >> t).fail();
  }
};

usage:

boost::bind< bool >(from_string< int >(), boost::ref(temp), _1)
于 2010-04-26T09:55:37.580 回答