【发布时间】:2013-05-19 19:42:49
【问题描述】:
我正在尝试在模板结构中使用一些 SFINAE。我将我的问题减少到以下问题,并且可以使这项工作:
template<bool mybool>
struct test {
void myfunc();
};
template<bool mybool>
void test<mybool>::myfunc() {
std::cout << "test true" << std::endl;
}
template<>
void test<false>::myfunc() {
std::cout << "test false" << std::endl;
}
int main(int argc, char ** argv) {
test<true> foo;
test<false> bar;
foo.myfunc();
bar.myfunc();
}
使用这段代码,我得到了结果:
test true
test false
但是,如果我想考虑我的struct test 具有多个模板参数,我尝试像这样调整上面的内容:
template<int myint, bool mybool>
struct test {
void myfunc();
};
template<int myint, bool mybool>
void test<myint,mybool>::myfunc() {
std::cout << "test true" << std::endl;
}
template<int myint>
void test<myint,false>::myfunc() {
//error: invalid use of incomplete type 'struct test<myint, false>'
std::cout << "test false" << std::endl;
}
int main(int argc, char ** argv) {
test<1,true> foo;
test<1,false> bar;
foo.myfunc();
bar.myfunc();
}
我对不完整类型“结构测试”的使用无效。
我是不是走错了方向?有没有办法做我想做的事? 感谢您的帮助!
【问题讨论】:
-
你写
foo.test()时是指foo.myfunc()吗? -
你在第二个例子中也拼错了
myfunc。应该是my_func。请在发布之前尝试您的示例。
标签: c++ templates template-specialization sfinae