Question

Is possible using traits to deduce if a template argument is Value or a Type?

template <typename A>
void function(){
    if(is_value<A>()::value)
        cout<<"A is value"<<endl;
    else
        cout<<"A is type"<<endl;
}

int main(){
    function<int>(); 
    function<3>();
}

outputs

"A is type"
"A is value"
Was it helpful?

Solution

Per 14.3/1 Standard:

There are three forms of template-argument, corresponding to the three forms of template-parameter: type, non-type and template.

And per 14.3.1/1 Standard:

A template-argument for a template-parameter which is a type shall be a type-id.

Since your template argument is type, you should pass a type-id as template argument. 3 is not a type-id. So, it is impossible in your way.

You could only add a function with non-type template-argument:

template <class A>
void function()
{
    std::cout << "A is type" << std::endl;
}

template <int A>
void function()
{
    std::cout << "A is value" << std::endl;
}

int main()
{
    function<int>();
    function<3>();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top