Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invoke template with `const&`?

Tags:

c++

templates

This appears to be legal

template<const int& x, int y>void fn() {}

However how do I call it? If it is the y type only fn<1>() seems to work (but not fn<intvar>()). The const& is confusing me especially when intvar doesn't seem to work with int y. Is this completely wrong/illegal? I'm using clang 3.2


1 Answers

External linkage is required for references & pointers for template parameters (for C++11, internal linkage, but static storage duration is required)

So if you have to use const int & as a template argument, you need to have it as extern in global scope.

extern const int a = 2;
fn<a,1>(); 
like image 173
P0W Avatar answered Feb 17 '26 05:02

P0W