Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specialization of template function after point of use will break the compilation

Consider next example :

#include <iostream>

template< int a >
void foo();

int main(int argn, char* argv[])
{
    foo<1>();
}

template<>
void foo<1>()
{
    std::cout<<1<<std::endl;
}

The compilation fails with next error messages :

rg.cpp:12: error: specialization of ‘void foo() [with int a = 1]’ after instantiation

What paragraph in the standard explains this error?

PS :I know that if I move the function definition in front of main will make the error go away.

like image 847
BЈовић Avatar asked May 06 '11 14:05

BЈовић


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 happens when a function is defined as a template?

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.

What is meant by template specialization Mcq?

Explanation: Template specialization is used when a different and specific implementation is to be used for a specific data type. In this program, We are using integer and character.

What is the main advantage of using template functions?

Some of the advantages of using templates are: Templates simplify the creation of documents. Templates can ease our workload and make us feel less stressed, and, at the same time, they increase efficiency. Templates increase the attention of the audience.


1 Answers

I think that's undefined behavior according to the standard. There are no restrictions on what a toolchain can do in cases of UB, generating a compiler error is one of the friendlier possibilities.


Section [temp.spec], 14.7p5 says

For a given template and a given set of template-arguments,

  • an explicit instantiation definition shall appear at most once in a program,
  • an explicit specialization shall be defined at most once in a program (according to 3.2), and
  • both an explicit instantiation and a declaration of an explicit specialization shall not appear in a program unless the explicit instantiation follows a declaration of the explicit specialization.

An implementation is not required to diagnose a violation of this rule.

Section [temp.expl.spec] 14.7.3p6 says:

If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.


Your program violates these requirements.

like image 98
Ben Voigt Avatar answered Sep 20 '22 04:09

Ben Voigt