Template specialisation in non include files

  • Thread starter Thread starter Andrew Ch. _
  • Start date Start date
A

Andrew Ch. _

Guest
I have been experimenting with template specializations and found that I can do something in Visual C++ 2015 that I did not expect. My question is, "Is this portable, or is this something that Visual C++ allows but is not standard?"

Here is my example in three files; main.cpp, Templates.h and Templates.cpp:

// Main.cpp

#include "stdafx.h"
#include "Templates.h"


int main()
{

Func( 3 ) ;
Func( 23.4f) ;
return 0;
}




// Templates.h


#ifndef Templates_h_Thu_28_Jun_2018__13_35_42
#define Templates_h_Thu_28_Jun_2018__13_35_42


template<typename T>
void Func( T Val ) = delete ;

template<>
void Func ( int Val ) ;

template<>
void Func ( float Val ) ;

#endif



// Templates.cpp



// System includes...
#include "StdAfx.h"
#include <iostream>

// Other includes...
#include "Templates.h"


using namespace std ;


template<>
void Func( int Val )
{
cout << "Int Func " << Val << endl ;
}

template<>
void Func( float Val )
{
cout << "Float Func " << Val << endl ;
}





I was surprised that this built, but having built it gave the "expected" output:

Int Func 3
Float Func 23.4

So, is this legitimate C++ code or is this a forgiving Visual C++ compiler?

(It would be nice if it is legal -- I have a more complex case in mind where I would like to delete (using "= delete") the primary template, and add allowed specialisations.)

Thanks in advance.

Andrew.

Continue reading...
 
Back
Top