初始化一个静态const变量给我非模板const的模板定义 [英] Initializing a static const variable gives me template definition of non-template const
问题描述
这是我的代码:
#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;
}
我尝试研究它,但我找不到任何东西,除了建议删除默认值重复。我想实现SFINAE,以确保第一个值是一个偶数。我有一个静态const变量,我想初始化。它工作正常没有我的默认类,但一旦我添加它,事情变得凌乱。如何初始化我的const静态成员?我也想知道为什么我的代码不工作。
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.
推荐答案
你需要专门化你的模板struct t
:
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;
}
};
使用它,你可以初始化你的变量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);
实例此处
这篇关于初始化一个静态const变量给我非模板const的模板定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!