【发布时间】:2017-03-17 08:48:17
【问题描述】:
我认为这不可能,但我想完全确定,所以我还是问了..
我想从一个没有在模板中传递但以其他方式注入的模板化结构(可用于 constexpr 函数)获取编译时间值。
很难解释,我用一些代码试试:
template<int A>
struct MagicStruct
{
enum { current = A, injected = /* magic */}
};
template<int A, int B>
struct InjectionStruct
{
enum { first=A, second=B}
/*... injection of B in MagicStruct<A> ... */
};
static const int AVALUE = 1;
static const int BVALUE = 2;
static const int CVALUE = InjectionStruct<AVALUE, BVALUE>::first; //== 1
static const int CVALUE = InjectionStruct<AVALUE, BVALUE>::second; //== 2
static const int DVALUE = MagicStruct<AVALUE>::injection; //== 2
是否有一些我不知道的技巧会允许这样做?
[edit] 我想只在输入中使用 AVALUE 作为模板参数来获取 DVALUE
【问题讨论】:
-
关于你的edit:如果你有20个
InjectionStruct,哪个定义了你的MagicStructinjected值?正如我的回答中所解释的,您需要在MagicStruct和提供数据的InjectionStruct之间建立关系。 -
好点 e 复杂的问题 :) 我知道这没有多大意义.. 但我认为它应该把评估/声明/定义/无论如何作为最后一个..
标签: c++ templates traits template-meta-programming constexpr