Защо компилаторът не може да специализира тази функция и има ли начин да го принудите да го направи?
Грешката, която получавам:
Грешка 1 грешка C2893: Неуспешно специализиране на шаблон на функция „неизвестен тип“ Ternary::check(bool,Left,Right)'
#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;
}
decltype
с g++ 4.5. Той се оплаква само от линиятаcheck_(Int2Type<condition>,left,right);
. - person kennytm   schedule 02.10.2010