2010-04-26 73 views
4

我有編譯下面的代碼片段升壓綁定功能以參考

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)); 

,其中有問題的功能是

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

我得到的錯誤是

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 

問題谷歌正在異常無用,所以我希望有人能提供一些見解。

UPDATE:問題是由@ GF的解決方案回答,但有趣的是,因爲它存儲的轉換操作(失敗/無故障),而不是轉換值本身的結果,最初的功能是不完全正確的。我改變了簽名直接返回轉換後的值,編譯器能夠正確推斷出綁定的類型。

回答

5

FromString()接受3個參數和默認參數是不是一個函數類型的一部分。所以綁定表達式應該大概是這樣的:

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

這正是問題所在!謝謝。這個錯誤信息是相當不合理的,不是:) – 2010-04-26 10:07:30

+0

@Jamie:既然這是解決方案,並且你已經接受了它,我認爲你同意這是一個很好的答案,因此應該是upvoted? – 2010-04-26 10:10:53

+0

@Jamie:我知道,Boost.Bind錯誤可能是最不具說明性的錯誤之一,VC cl.exe甚至可能會讓這些錯誤崩潰。 – 2010-04-26 10:17:10

0

boost :: bind不支持函數模板。你應該把函數變成一個函數對象:

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

用法:

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

的OP不流通的模板功能,而是它的一個具體實例 - 'FromString ()'。 – 2010-04-26 10:19:24

+0

@gf:我認爲它是一個函數模板,因爲OP的函數定義片段。 我還假設語法&FromString < int >不是一個可移植的語法。 – VuuRWerK 2010-04-26 11:03:52

+0

我向你保證,它的可移植標準兼容語法和模板函數的具體實例與函數指針上下文中的常見語法沒有區別。 – 2010-04-26 11:09:00