Link the function using the link

I'm having trouble 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 is the function in question

 // 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(); } 

I get an error

 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 unusually useless, so I hope someone here can give some ideas.

UPDATE : @gf's answer answers the question, but it is interesting that the original function was not entirely correct, since it saved the result of the conversion operation (failed / failed), and not the converted value itself. I changed the signature, returned the converted value directly, and the compiler was able to correctly determine the type of binding.

 // 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
source share
2 answers

FromString() takes 3 arguments, and the default arguments are not part of the function type. So the bind expression should be something like this:

 boost::bind<int>(&FromString<int>, boost::ref(temp), _1, std::dec); 
+5
source

Function templates are not supported by boost :: bind. You must include the function in the 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(); } }; 

using:

 boost::bind< bool >(from_string< int >(), boost::ref(temp), _1) 
0
source

Source: https://habr.com/ru/post/1308005/


All Articles