C++ 从非类型模板参数确定类型
我希望我能做到这一点:C++ 从非类型模板参数确定类型,c++,templates,member-function-pointers,C++,Templates,Member Function Pointers,我希望我能做到这一点: template <typename T x> struct Test { T val{x}; }; int main() { Test<3> test; return test.val; } 或 模板 那么类型就无法确定了 接下来我尝试了专业化: template <typename T, typename T2> struct Accessor; template <typename V, t
template <typename T x>
struct Test {
T val{x};
};
int main() {
Test<3> test;
return test.val;
}
或
模板
那么类型就无法确定了
接下来我尝试了专业化:
template <typename T, typename T2>
struct Accessor;
template <typename V, typename T, typename VP>
struct Accessor <V(T::*)(), void (T::*)(VP)>
模板
结构存取器;
模板
结构存取器
如果使用,将确定所有类型
typedef Accessor<
decltype(&TargetClass::GetFoo),
decltype(&TargetClass::SetFoo)> fooAcessor;
typedef存取器<
decltype(&TargetClass::GetFoo),
decltype(&TargetClass::SetFoo)>fooAcessor;
但是现在我没有指针了,只有类型
是否有一种编写模板的方法,以便可以从非类型模板参数自动确定类型 是否有一种编写模板的方法,以便可以从非类型模板参数自动确定类型 在C++17中,是的,这要归功于:
模板
结构测试{
decltype(x)val{x};
};
在C++17之前,没有。您必须编写:
template <class T, T x>
struct Test {
T val{x};
};
模板
结构测试{
T val{x};
};
@Quentin Ahem!哎呀。被破坏的封面被破坏了,我不知道狗的事:(但是,嘿,有魔力,还有C++,它是一只狗,就像只有25%的失败!
template <typename T, typename T2>
struct Accessor;
template <typename V, typename T, typename VP>
struct Accessor <V(T::*)(), void (T::*)(VP)>
typedef Accessor<
decltype(&TargetClass::GetFoo),
decltype(&TargetClass::SetFoo)> fooAcessor;
template <auto x>
struct Test {
decltype(x) val{x};
};
template <class T, T x>
struct Test {
T val{x};
};