具有非类型模板参数的方法
Methods with non-type template parameters
我想知道创建非类型模板化类方法的正确语法是什么。我已经试过这个,但显然它不起作用:
class A
{
enum B
{
C = 0,
D
};
template <A::B value = A::C>
int fun();
};
template<A::B value>
int A::fun<A::B::C>()
{
return 1;
}
template<A::B value>
int A::fun<A::B::D>()
{
return fun<B>() + 1;
}
我做错了什么?
问题是您的专用化语法不正确。 它正在尝试对函数进行部分特化,但这在这里甚至没有意义 - 无论如何都是不允许的。
您还尝试在第二个专用化中调用fun<B>()
,但B
是类型名而不是枚举的值,因此无法解析调用。
试试这个:
// Removed template argument to make a complete specialization instead of partial.
template<>
int A::fun<A::B::C>()
{
return 1;
}
// Removed template argument to make a complete specialization instead of partial.
template<>
int A::fun<A::B::D>()
{
// Changed template argument from B (which is a type) to C (which is a value of
// type B.
return fun<C>() + 1;
}
您正在尝试部分专用化函数模板,这是不允许的。这是一个可编译的代码片段:
class A
{
enum B
{
C = 0,
D
};
template <A::B value = A::C>
int fun();
};
template<>
int A::fun<A::B::C>()
{
return 1;
}
template<>
int A::fun<A::B::D>()
{
return fun<B::C>() + 1;
}
相关文章:
- 使用在用于SFINAE的void_t中具有参数的方法
- 如何制作一个将函数作为参数的类方法
- c++方法参数只能在linux的发布模式下自行更改
- 使用用户定义的参数调用future/async并调用类方法
- 将成员函数指针作为参数传递给模板方法
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 具有字符串化的可变参数宏的现代/通用方法
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 如何在方法中传递结构参数
- 如何将类成员方法的参数列表自动填充写入可变参数?
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- C++接口继承不同的参数方法
- C++ 模板:重载时找不到基类类型参数方法
- 如何使用迭代器作为参数方法?
- 具有不同亚型的参数方法
- 如何以相同的方法在子类中超载的相同方法来实现工作变量参数方法
- 如何将 C 中声明的可选参数方法转换为 c++
- 如果模板化"ResourceCache"需要不同的创建参数/方法,它们如何加载/创建 T 类型的资源?