Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Andrey_K_
Beginner
208 Views

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

Jump to solution

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
208 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

3 Replies
Judith_W_Intel
Employee
208 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...

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

Judy

Judith_W_Intel
Employee
209 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

J_B_
Beginner
208 Views

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

Reply