Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

(c++11) issues with using pointer to local function as template argument

Andrey_K_
Beginner
749 Views

Hi,

Trying to compile the following code:

#include <iostream>
using namespace std;


template<void (*F)()> void SetCallback(){
	F();
}

static void Foo() {
	struct stub_t {
		static void cb(){
			cout << "Foo::stub_t::cb()" << endl;
		}
	};
	SetCallback<&stub_t::cb>();
}

void TemplateInstantiation() {
	Foo();
}

I get this error: "(18): error : a template argument may not reference a non-external entity".

But I can't find any restrictions on template arguments in c++11 specification, at least in draft n3376, that can explain why this is wrong.

Is there something that I have been missed? Or this is specific of the compiler?

I'm using Intel® C++ Composer XE 2015.

0 Kudos
1 Solution
Judith_W_Intel
Employee
749 Views

 

I take that back... Actually it looks like Microsoft has always allowed it (even before it was legal).

I have entered a defect (DPD200256284) for us to change our compiler to allow this. Thanks for reporting it.

Judy

 

View solution in original post

0 Kudos
3 Replies
Judith_W_Intel
Employee
749 Views

 

Yes it looks like this was changed between C++03 and C++11 -- see discussion here:

http://stackoverflow.com/questions/14597463/why-did-c03-require-template-parameters-to-have-external-linkage

It looks like no compiler (including the most recent releases of Clang, GNU, Microsoft and our compiler) has implemented this change yet.

Judy

0 Kudos
Judith_W_Intel
Employee
750 Views

 

I take that back... Actually it looks like Microsoft has always allowed it (even before it was legal).

I have entered a defect (DPD200256284) for us to change our compiler to allow this. Thanks for reporting it.

Judy

 

0 Kudos
J_B_
Beginner
749 Views

Anyway (like a compiler option) to circumvent this in the meantime without a code rewrite? 

0 Kudos
Reply