我想定义一个nullary静态模板成员函数,该函数将(明确地)专门用于指向数据成员的指针,并且对于每个特化,可以具有不同的返回类型。
它应返回有关每个属性的一些详细信息,因此我将调用此方法trait
。返回的特征对象类型将由其他模板检查,因此整个机器必须在编译时可用。
到目前为止,我有类似的东西(当然是破碎的代码):
class Foo{
// some data members
int a; std::string b; int c;
// how to declare generic template here?
// compile-time error should ensue if none of the specializations below is matched
// specialization for a (aTraitExpr is expanded from macro, so it is OK to repeat it)
template auto trait<&Foo::a>()->decltype(aTraitExpr){ return aTraitExpr; }
// specialization for b; the return type will be different than for trait<&Foo::a>
template auto trait<&Foo::b>()->decltype(bTraitExpr){ return bTraitExpr; }
};
// some code which queries the trait at compile-time
// e.g. supposing all possible trait types declare isSerializable
// which happens to be True for a and False for b
Foo* foo;
template<bool isSerializable> void doSerialization(...);
template void doSerialization<true>(...){ ... };
template void doSerialization<false>(...){ /* no-op */ };
doSerialization<Foo::trait<&Foo::a>()::isSerializable>(...); // -> doSerialization<true>(foo)
doSerialization<Foo::trait<&Foo::b>()::isSerializable>(...); // -> doSerialization<False>(...)
doSerialization<Foo::trait<&Foo::c>()::isSerializable>(...); // -> compile error, specialization Foo::trait<&Foo::c> not defined
可以获得一些如何实现这一目标的提示吗? (我不打算发明一个新的序列化系统,我已经使用了boost :: serialization;每个特性中都会有更多的信息,这只是为什么在编译时需要它的例子。)
编辑:我能够得到一些接近我想要的东西,它显示为at ideone.com。我放弃了trait<Foo::a>()
(现在),所以有静态函数getTrait_a()
,它返回对可修改类型特征的引用,但是它们在编译时被部分修复(因此Foo::TraitType_a::flags
例如,工作)。感谢所有回复的人,不幸的是我只能选择其中一个答案作为“回答”。
答案 0 :(得分:3)
看起来你想要几个重载而不是专业化。不幸的是,您没有详细说明xTraitExpr
是什么,但它似乎只是一个定义了成员isSerializable
的类型。我可能会喜欢这个
class Foo {
// your members have been omitted to save space...
template<typename T, T Foo::*M>
struct W { };
static decltype(aTraitExpr) trait(W<int, &Foo::a>) {
return aTraitExpr;
}
static decltype(bTraitExpr) trait(W<std::string, &Foo::b>) {
return bTraitExpr;
}
// other overloads for other members...
public:
// overloads for each member type
template<int Foo::*M>
static decltype(trait(W<int, M>())) trait() {
return trait(W<int, M>());
}
template<std::string Foo::*M>
static decltype(trait(W<std::string, M>())) trait() {
return trait(W<std::string, M>());
}
};
trait(W<M>())
是一个从属电话。依赖调用在定义和实例化时执行ADL,仅在定义时执行非限定查找。这就是W
和使用它的额外trait
重载必须在trait
类型重载之前而不是在它们之后定义的原因,或者函数的返回类型和主体中的解析结果是不同的,因为它们在不同的时间被解析(在类定义之后主体被解析,并且立即解析返回类型)。
您可以使trait
成为constexpr
函数,并使xTraitExpr
成为一个文字类,其中constexpr构造函数正确初始化isSerializable
,或者您可以应用decltype
如下
doSerialization<decltype(Foo::trait<&Foo::a>())::isSerializable>(...);
答案 1 :(得分:2)
我认为在这里使用功能模板没有意义。话虽这么说,使用类模板代替它也不方便:你必须考虑到非静态数据成员可以有不同类型的事实,并且可以有几个具有相同类型的非静态数据成员。这是一种可能性:
template<typename T>
struct is_serializable: std::false_type {};
struct Foo {
int a; std::string b; int c;
// Primary template left undefined on purpose
// alternatively, could use a static_assert on a dependent
// std::false_type::value for better diagnostics
template<typename T, T t>
struct attribute_trait;
};
// Define explicit specializations outside of class
template<>
struct Foo::attribute_trait<int Foo::*, &Foo::a>
: is_serializable<int> {};
template<>
struct Foo::attribute_trait<std::string Foo::*, &Foo::b>
: is_serializable<std::string> {};
可用作
doSerialization<Foo::attribute_trait<decltype(&Foo::a), &Foo::a>::value>(/* stuff */);
答案 2 :(得分:1)
定义traits类的常用方法是将struct /类包装在编译时常量表达式周围(而不是通过包装返回此类表达式的函数)。采用类成员函数的语法如下:
template
<
SomeReturnType (SomeClass::*SomeMemberFunction)(SomeParameters)
>
class SomeTrait
{
static const value = SomeCompileTimeConstantExpression;
};
在你的情况下,你会这样做:
template
<
void (Foo::*f)()
>
class trait
{
static const value = fTraitExpr;
};
然后为class Foo
的所有成员函数专门化此特征:
template<>
class trait<&Foo::a>
{
static const value = aTraitExpr;
};
// same for Foo::b and Foo::c
此外,重载功能模板比专门化更为惯用:
template<int V> struct Int2Type { enum { value = V }; };
Foo* foo;
template
<
void (Foo::*f)()
>
void doSerialization(...)
{
dispatch::doSerialization(Int2Type< trait<f>::value >(), ...);
}
namespace dispatch {
doSerialization(Int2Type< true >, ...) { ... };
doSerialization(Int2Type< false >, ...) { /* no-op */ };
} // namespace dispatch
然后你可以这样称呼它:
doSerialization<&Foo::a>(...);
doSerialization<&Foo::b>(...);
doSerialization<&Foo::c>(...);