Initializing a static const variable gives me template definition of non-template const

Here is my code :

#include <iostream> class c{ public: int y; c(int x,int p):y(x){ std::cout<<"has been made"<<std::endl; } }; template<int x,typename B = void> struct checkeven{ }; template<typename B> struct checkeven<0,B>{ typedef B type; }; template<int x,int y,class = typename checkeven<x%2,int>::type> struct t{ static const c tee; static const inline c& initor(){ return tee; } }; template<int x,int y> const c t<x,y>::tee(x,y); //how do i initialize? int main(int argc, char** argv) { //t<2,1>::initor(); //t<2,2>::initor(); return 0; }

I tried researching it about but i can't find anything except the suggestion to remove the default value duplicate. I want to implement SFINAE to make sure that the first value is an even number. I have a static const variable that i want to initialize. It was working fine without my default class but once i added it, things got messy. How do i initialize my const static member? I would also like to know why my code isn't working.

Answer1:

You need to specialize your template struct t like that :

template<int x, int y> struct t<x,y, typename checkeven<x%2,int>::type> { static const c tee; static const inline c& initor(){ return tee; } };

Using this you'll be able to initialize your variable tee like that :

template<int x, int y> const c t<x, y>::tee(x, y);

Live example here

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Initializing a static const variable gives me template definition of non-template const