[英]partial template specialization for non type member function pointer values
I would like to partially specialize a structure for a non-type template parameter, specifically for member function pointers known at compile time.我想为非类型模板参数部分专门化一个结构,特别是在编译时已知的成员 function 指针。 As an example, I start with
int
and non int
values and that works fine and prints例如,我从
int
和非int
值开始,效果很好并且可以打印
false true
but when I un-comment adap
, I get the following compile error但是当我取消注释
adap
时,我得到以下编译错误
callable.cc:9:39: error: template argument 1 is invalid
9 | struct adap<Ret(Class::*Fptr)(Args...)> : std::false_type {};
#include <functional>
#include <iostream>
using namespace std;
/* Comment out
template <auto T>
struct adap : std::true_type {};
template <typename Ret, typename Class, typename ...Args>
struct adap<Ret(Class::*Fptr)(Args...)> : std::false_type {};
*/
template <auto T>
struct A {
static constexpr bool value = true;
};
template <int N>
struct A<N> {
static constexpr bool value = false;
};
struct B {
void f() {
}
};
int main() {
std::cout << boolalpha << A<4>::value << " " << A<char{'a'}>::value << endl;
// adap<&B::f> ad;
}
When you create a partial specialization for a template, you have to provide template argument(s) to the primary specialization that signal when your partial specialization should be used.当您为模板创建部分特化时,您必须向主特化提供模板参数,以指示何时应该使用您的部分特化。 The primary template for
adap
takes a non-type template parameter: a value. adap
的主模板采用非类型模板参数:一个值。 So your partial specialization needs to provide a value.所以你的部分专业化需要提供一个价值。
Which it doesn't.它没有。
Fptr
is not a value. Fptr
不是一个值。 I'm guessing that you intend it to be the name of a parameter.我猜你打算将它作为参数的名称。 But you're not providing a parameter;
但是您没有提供参数; you're providing an argument , one that fits the corresponding template parameter defined in the primary template.
您正在提供一个参数,它适合主模板中定义的相应模板参数。 That parameter being an NTTP, you must provide a value .
该参数是 NTTP,您必须提供一个value 。 The idea being that when a user provides that value, your specialization will take over.
这个想法是,当用户提供该值时,您的专业化将接管。
auto
-deduced NTTP parameters were added so that you could avoid having to do something like this: template<typename T, T value>
.添加了
auto
推导的 NTTP 参数,这样您就可以避免必须执行以下操作: template<typename T, T value>
。 Your specialization needs to basically bring this back.你的专业化需要基本上把它带回来。 Your template header should take the component types of a member pointer (return type, class type, and arguments) and an NTTP value which is a member pointer type that uses these components.
您的模板 header 应该采用成员指针的组件类型(返回类型、class 类型和参数)和NTTP 值,NTTP 值是使用这些组件的成员指针类型。 You then pass that value to the primary template:
然后将该值传递给主模板:
template <auto T>
struct adap : std::true_type {};
template <typename Ret, typename Class, typename ...Args, Ret(Class::*value)(Args...)>
struct adap<value> : std::false_type {};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.