爲什麼編譯器不能專門化這個函數,是否有辦法強制他這麼做?
我得到的錯誤:
錯誤1個錯誤C2893:無法專注函數模板'未知類型 '三元::檢查(BOOL,左,右)'三元運算符
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::string;
template<int v>
struct Int2Type
{
enum {value = v};
};
template<bool condition,class Left, class Right>
struct Result;
template<class Left, class Right>
struct Result<true,Left,Right>
{
typedef Left value;
};
template<class Left, class Right>
struct Result<false,Left,Right>
{
typedef Right value;
};
struct Ternary
{
template<class Left, class Right>
static Right check_(Int2Type<false>, Left left, Right right)
{
return right;
}
template<class Left, class Right>
static Left check_(Int2Type<true>, Left left, Right right)
{
return left;
}
__Updated__
template<bool Condition,class Left, class Right>
static auto check(Left left, Right right)->
typename Result<Condition,Left,Right>::value
{
return check_(Int2Type<Condition>,left,right);
}
int _tmain(int argc, _TCHAR* argv[])
{
int a = 5;
string s = "Hello";
cout << Ternary::check<false>(a,s);
return 0;
}
@Prasoon Saurav C++ 03它可以被認爲是C++ 0x的一個「子集」,所以你的編輯不適合C++ 0x覆蓋它。 – 2010-10-02 12:08:17
是的,但是SO並不那麼明亮。如果您搜索標有「C++」的問題,SO將不會列出此問題。 – 2010-10-02 12:12:32
什麼是錯誤? – ybungalobill 2010-10-02 12:13:42