2

I am on a project where I have to use a reference to a constant templated object as a parameter of another object's template.

Simply put I would like to do this:

template<typename T>
class A {...}

template<typename T, A<T>& a>
class B {...}

int main(){
   const A<int> a;
   B<int, a> b;

Problem is I can't figure out how to make it happen and I need your help.

On visual studio, the above code will produce the following error : "C2971: a variable with non-static storage duration cannot be used as a non-type argument"

If I try to use constexpr instead of const, with the following changes :

constexpr A<int> &a = A<int>(3);
B<int,a> b;

I get the following error "C2131: expression did not evaluate to a constant"

Well I did try a few things that I saw on other posts regarding similar matters but unfortunately did not find anything that could solve my problem.

I am most certainly doing something wrong but can't figure out what.

Thank you in advance for your help!

EDIT : I tried the answer but unfortunately, even if it seems to be ok at first, I got this error while compiling : C2970 : an expression involving objects with internal linkage cannot be used as a non-type argument. Which appear to suggest that I cannot use the template parameter in another file? (I separated all the classes in different hpp) Any ideas ?

SOLVED: To solve the problem I used the solution hereunder and (I assume because my class was in a separate hpp file) just put the keyword extern before it to have :

extern const A<int> a;
int main(){ B<int, a> b; }

And it works like a charm.

docky
  • 33
  • 1
  • 1
  • 6

1 Answers1

2

You can't use local variables as parameters for template - type definition cannot be localized, it can be used outside local variable scope. But if you replace your template param to reference to global variable that would work:

template<typename T>
class A {
  public:
    A() {}
};

template<typename T, A<T>& a>
class B {
  public:
    B() {}
};

A<int> a;
int main(){
  B<int, a> b;
}
Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46
  • The OP is using a non-static variable so this answer does not cover the OPs issue. – Shafik Yaghmour Nov 20 '15 at 13:28
  • that why VS says it's error - and solution is not use local variables – Andrey Starodubtsev Nov 20 '15 at 13:50
  • With no explanation this is a code only answer and thus a poor one. – Shafik Yaghmour Nov 20 '15 at 13:51
  • Thank you for your answer Andrey. Is there any possibility to find a solution that keeps the A a; inside the main ? – docky Nov 20 '15 at 13:52
  • So `a` now has static storage duration, now the problem is getting `a` to be non-local. – Shafik Yaghmour Nov 20 '15 at 15:03
  • @AndreyStarodubtsev Unfortunately, even if it seems to be ok at first, I got this error while compiling : C2970 : an expression involving objects with internal linkage cannot be used as a non-type argument. Which appear to suggest that I cannot use the template parameter in another file? (I separated all the classes in different hpp) Any ideas ? – docky Nov 20 '15 at 22:39
  • hmmm.. I declared object, reference to which I want to use as parameter to template as `extern A a;` in header with templates and it compiles ok. Even if it's declared in separate header, it still works. I use g++ (GCC) 4.8.1, by the way, have no VS under the hand, may be that's an issue? – Andrey Starodubtsev Nov 21 '15 at 13:14