Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there provision specifying the restriction on "return type" in a function template explicit specialization?

template<class T>
void fun(T){}

template<>
int fun(int){return 0;}

Consider this example, it is rejected by all implementations. However, I haven't found any persuasive provision in the current standard that specifies this explicit specialization declaration is ill-formed. If it exists, what is the rule?

In addition, the potential relevant rule may be that [temp.deduct.decl#2]

If, for the set of function templates so considered, there is either no match or more than one match after partial ordering has been considered ([temp.func.order]), deduction fails and, in the declaration cases, the program is ill-formed.

I think the meaning of "match" is not sufficiently clear here since "match" didn't clearly define anything.

like image 608
xmh0511 Avatar asked Dec 22 '21 11:12

xmh0511


People also ask

What is function template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is difference between class template and function template in C++?

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.

What is the need of template functions in C?

Overall, implementing templates in C can make C code more readable, less redundant, and less prone to errors. It allows efficient development without the need to incorporate or switch to C++ or languages with builtin template systems, if desired.

Can a template function have a non-template return type?

Like any other ordinary functions, the template functions can have a non-template return type, i.e., any built-in type (int, double, char, etc.) or user-defined data type (struct or class type). In the following section, we will see an example of the built-in type used as the return type.

Do I need C++14 to use Template functions?

Some of the examples in this chapter will require support of C++14 or higher compiler. Like any other ordinary functions, the template functions can have a non-template return type, i.e., any built-in type (int, double, char, etc.) or user-defined data type (struct or class type).

Can a template function return a void in C++?

Declaring a separate type parameter for specifying the return type of a template function is mandatory in C++. [True/False] A template function must return a value and hence the use of void as return type is not possible. [True/False]


2 Answers

Your template definition did not match, as void fun(T) is not T fun(T) in your specialization or maybe the other way around if you have int fun(T) to specialize with int fun(int).

You simply have to change to:

template<class T>
T fun(T){}

template<>
int fun(int){}

BTW: All this results in a lot of warning because you did not return anything :-)

Why it did not match:

template<class T>
void fun(T) {}

expands for T=int to:

template<class T>
void fun(int) {}

BUT: the specialization ( it isn't one, because it did not match )

template <>
int fun(int){return 0;}

has a return type which can never deduced from the original template definition, as this, it is never a specialization because it always has return type void while your specialization has int.

like image 108
Klaus Avatar answered Oct 23 '22 05:10

Klaus


You're on the right track with your quote. Let's also consider the following text:

In all these cases, P is the type of the function template being considered as a potential match and A is ... the function type from the declaration ... The deduction is done as described in [temp.deduct.type].

What are these P and A types? From [temp.deduct.type]

an attempt is made to find template argument values that will make P, after substitution of the deduced values (call it the deduced A), compatible with A.

There's just no value of T that will make A = int fun(int) compatible with P = void fun(T).

like image 5
MSalters Avatar answered Oct 23 '22 06:10

MSalters