tags:

views:

105

answers:

3

What am I doing wrong?

#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;
    }

    template<class Left, class Right>
    static auto check(bool condition, Left left, Right right)-> decltype(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;
}

I'm getting error:
"Error 1 error C2893: Failed to specialize function template ''unknown-type' Ternary::check(bool,Left,Right)'"

Why? EDIT

   template<class Left, class Right>
    static auto check(bool condition, Left left, Right right)-> 
        decltype(Result<(sizeof(int) == 1),Left,Right>::value)
    {
        return check_(Int2Type<condition>,left,right);
    }

added:

Result<(sizeof(int) == 1)
A: 
static auto check(bool condition, Left left, Right right)-> decltype(Result<condition,Left,Right>::value)

What???

You can't use run-time variable condition to parametrize a template at compile time!?

ybungalobill
Perhaps he meant `typeof (condition)`? err... but that won't work everywhere, will it?
FrustratedWithFormsDesigner
No, first parameter of Result is `bool value`!
ybungalobill
+2  A: 

condition here is a variable of your program.

A template parameter must be a constant expression (or a type), and therefore a variable is not suitable since the compiler cannot select the right specialization based on runtime information.

Matthieu M.
A: 

Just write (after removing check):

cout << Ternary::check_(IntToType<false>,a,s);
paul_71
@paul_71 yes but I want correct check_ to be selected according to first parameter.
There is nothing we can do
@There: You *can't* have the return type of a function determined at runtime in C++.
UncleBens
@There is nothing... (hoo what a nick!) that's exactly what's gonna happen: In auto tResult = Ternary::check_(IntToType<false>, a, s); tResult will be of type 'Right', in your example std::string as you can easily verify by deltype(tResult) tOther = "surely a string". As has been stated from other posters, you cannot use a run-time variable (condition) as a class template id.
paul_71