Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 结构中的类型注入_C++_Templates_Traits_Template Meta Programming_Constexpr - Fatal编程技术网

C++ 结构中的类型注入

C++ 结构中的类型注入,c++,templates,traits,template-meta-programming,constexpr,C++,Templates,Traits,Template Meta Programming,Constexpr,我想我所问的是不可能的,但我想完全肯定,所以我还是问了 我想从模板结构(在constexpr函数中可用)中获取编译时值,该结构未在模板中传递,但以其他方式注入 这很难解释,我将尝试使用一些代码: template<int A> struct MagicStruct { enum { current = A, injected = /* magic */} }; template<int A, int B> struct InjectionStruct { enum {

我想我所问的是不可能的,但我想完全肯定,所以我还是问了

我想从模板结构(在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
模板
结构MagicStruct
{
枚举{current=A,injected=/*magic*/}
};
模板
结构注入结构
{
枚举{first=A,second=B}
/*…在MagicStruct中注入B*/
};
静态常数int AVALUE=1;
静态常量int BVALUE=2;
静态常量int CVALUE=InjectionStruct::first;//=1.
静态常量int CVALUE=InjectionStruct::second;//==2.
静态const int DVALUE=MagicStruct::injection;//=2.
是不是有什么我不知道的把戏可以让你这么做


[编辑]我想在输入中使用AVALUE作为模板参数来获取DVALUE以下内容是否有助于您

template<int A, int B>
struct InjectionStruct
{
    constexpr static int first = A;
    constexpr static int second = B;
    constexpr static int MAGIC = B;
};

template<int A, class Injection>
struct MagicStruct
{
    constexpr static int current = A;
    constexpr static auto injected = Injection::MAGIC;
};
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, InjectionStruct<AVALUE, BVALUE>>::injected; //== 2
模板
结构注入结构
{
constexpr static int first=A;
constexpr static int second=B;
constexpr static int MAGIC=B;
};
模板
结构MagicStruct
{
constexpr静态int电流=A;
constexpr静态自动注入=注入::魔术;
};
静态常数int AVALUE=1;
静态常量int BVALUE=2;
静态常量int CVALUE=InjectionStruct::first;//=1.
静态常量int CVALUE=InjectionStruct::second;//==2.
静态const int DVALUE=MagicStruct::injected;//=2.

如果不知道要注入什么,很难给出更深入的建议。

以下内容对您有帮助吗

template<int A, int B>
struct InjectionStruct
{
    constexpr static int first = A;
    constexpr static int second = B;
    constexpr static int MAGIC = B;
};

template<int A, class Injection>
struct MagicStruct
{
    constexpr static int current = A;
    constexpr static auto injected = Injection::MAGIC;
};
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, InjectionStruct<AVALUE, BVALUE>>::injected; //== 2
模板
结构注入结构
{
constexpr static int first=A;
constexpr static int second=B;
constexpr static int MAGIC=B;
};
模板
结构MagicStruct
{
constexpr静态int电流=A;
constexpr静态自动注入=注入::魔术;
};
静态常数int AVALUE=1;
静态常量int BVALUE=2;
静态常量int CVALUE=InjectionStruct::first;//=1.
静态常量int CVALUE=InjectionStruct::second;//==2.
静态const int DVALUE=MagicStruct::injected;//=2.

在不知道要注入什么的情况下,很难给出更深入的建议。

您需要在
注入结构
MagicStruct
之间建立某种关系。因此,我在
InjectionStruct
中为
injected
值添加了一个额外的模板参数和一个
typdef

第一个示例使用
InjectionStruct
本身来定义特定的MagicStruct:

例如:

// MagicStruct is generic
template<int A, int INJ>
struct MagicStruct
{
    enum { current = A, injected = INJ};

};

// Injection struct has it own copy of MagicStruct
template<int A, int B>
struct InjectionStruct
{
    enum { first=A, second=B};

    // Provide custom magic number
    constexpr static int provideInjection(){ return B;}

    // Own copy of magicStruct, injected by this template instance.
    template <int X> struct MagicStruct: public ::MagicStruct<X,provideInjection()>{};
};

constexpr static const int AVALUE = 1;
constexpr static const int BVALUE = 2;

constexpr static const int CVALUE1 = InjectionStruct<AVALUE, BVALUE>::first; //== 1
constexpr static const int CVALUE2 = InjectionStruct<AVALUE, BVALUE>::second; //== 2
// Using a specific InjectionStruct, provies the MagicStruct with one argument.
constexpr static const int DVALUE3 = InjectionStruct<AVALUE, BVALUE>::MagicStruct<AVALUE>::injected; //== 2


int main()
{
    std::cout << CVALUE1 << std::endl;
    std::cout << CVALUE2 << std::endl;
    std::cout << DVALUE3 << std::endl;

    return 0;
}

您需要在
InjectionStruct
MagicStruct
之间建立某种关系。因此,我在
InjectionStruct
中为
injected
值添加了一个额外的模板参数和一个
typdef

第一个示例使用
InjectionStruct
本身来定义特定的MagicStruct:

例如:

// MagicStruct is generic
template<int A, int INJ>
struct MagicStruct
{
    enum { current = A, injected = INJ};

};

// Injection struct has it own copy of MagicStruct
template<int A, int B>
struct InjectionStruct
{
    enum { first=A, second=B};

    // Provide custom magic number
    constexpr static int provideInjection(){ return B;}

    // Own copy of magicStruct, injected by this template instance.
    template <int X> struct MagicStruct: public ::MagicStruct<X,provideInjection()>{};
};

constexpr static const int AVALUE = 1;
constexpr static const int BVALUE = 2;

constexpr static const int CVALUE1 = InjectionStruct<AVALUE, BVALUE>::first; //== 1
constexpr static const int CVALUE2 = InjectionStruct<AVALUE, BVALUE>::second; //== 2
// Using a specific InjectionStruct, provies the MagicStruct with one argument.
constexpr static const int DVALUE3 = InjectionStruct<AVALUE, BVALUE>::MagicStruct<AVALUE>::injected; //== 2


int main()
{
    std::cout << CVALUE1 << std::endl;
    std::cout << CVALUE2 << std::endl;
    std::cout << DVALUE3 << std::endl;

    return 0;
}

您可以将
注入到
静态int
中:

template<int A>
struct MagicStruct
{
    enum { current = A };
    static int injected;
};

template<int A>
int MagicStruct<A>::injected;
然后要执行注入,只需在某个地方使用
inject
,或者显式地实例化它:

static const int AVALUE = 1;
static const int BVALUE = 2;

static const int CVALUE = InjectionStruct<AVALUE, BVALUE>::first; //== 1
static const int DVALUE = InjectionStruct<AVALUE, BVALUE>::second; //== 2
//explicit instantiation
template InjectionStruct<AVALUE,BVALUE>::filler
         InjectionStruct<AVALUE,BVALUE>::inject;
static const int EVALUE = MagicStruct<AVALUE>::injected; //== 2
您对该实现的使用几乎完全符合您的要求:

static const int AVALUE = 1;
static const int BVALUE = 2;

static const int CVALUE = InjectionStruct<AVALUE, BVALUE>::first(); //== 1
static const int DVALUE = InjectionStruct<AVALUE, BVALUE>::second(); //== 2
static const int EVALUE = MagicStruct<AVALUE>::injected(); //== 2
static const int AVALUE=1;
静态常量int BVALUE=2;

static const int CVALUE=InjectionStruct

您可以将
注入到
静态int
中:

template<int A>
struct MagicStruct
{
    enum { current = A };
    static int injected;
};

template<int A>
int MagicStruct<A>::injected;
然后要执行注入,只需在某个地方使用
inject
,或者显式地实例化它:

static const int AVALUE = 1;
static const int BVALUE = 2;

static const int CVALUE = InjectionStruct<AVALUE, BVALUE>::first; //== 1
static const int DVALUE = InjectionStruct<AVALUE, BVALUE>::second; //== 2
//explicit instantiation
template InjectionStruct<AVALUE,BVALUE>::filler
         InjectionStruct<AVALUE,BVALUE>::inject;
static const int EVALUE = MagicStruct<AVALUE>::injected; //== 2
您对该实现的使用几乎完全符合您的要求:

static const int AVALUE = 1;
static const int BVALUE = 2;

static const int CVALUE = InjectionStruct<AVALUE, BVALUE>::first(); //== 1
static const int DVALUE = InjectionStruct<AVALUE, BVALUE>::second(); //== 2
static const int EVALUE = MagicStruct<AVALUE>::injected(); //== 2
static const int AVALUE=1;
静态常量int BVALUE=2;

static const int CVALUE=InjectionStruct

感谢您的尝试:)查看我编辑的帖子。在我的例子中,我只想通过使用AVALUE获得DVALUE。DVALUE本质上是BVALUE的别名,因此它将始终依赖于BVALUE。我认为您想要的是,编译器查看所有InjectionStruct的定义,并根据AVALUE选择正确的定义。这种行为是不可能的,abd即使可能,您也不会想这样做,因为它不是一对一映射。我很抱歉这么说,但是你应该考虑一个不同的设计。我只是在这里做实验,所以没有设计问题。我只是想要一种在模板MagicStruct中获取编译时值的方法,该值不是用该模板值插入的。谢谢:)是的,第二种方法有效。不幸的是,对于我所想的,它还不够通用。。每次都必须定义struct MagicStruct。好办法anyway@user3770392:请解释“您每次都必须定义结构MagicStruct”。谢谢您的尝试:)查看我编辑的文章。在我的例子中,我只想通过使用AVALUE获得DVALUE。DVALUE本质上是BVALUE的别名,因此它将始终依赖于BVALUE。我认为您想要的是,编译器查看所有InjectionStruct的定义,并根据AVALUE选择正确的定义。这种行为是不可能的,abd即使可能,您也不会想这样做,因为它不是一对一映射。我很抱歉这么说,但是你应该考虑一个不同的设计。我只是在这里做实验,所以没有设计问题。我只是想要一种在模板MagicStruct中获取编译时值的方法,该值不是用该模板值插入的。谢谢:)是的,第二种方法有效。不幸的是,对于我所想的,它还不够通用。。每次都必须定义struct MagicStruct。好办法anyway@user3770392:请解释“每次都必须定义结构MagicStruct”。谢谢:)请查看我编辑的帖子。在我的例子中,我只想通过使用AVALUE获得DVALUE。当我使用MagicStruct时,我不知道BVALUE(谢谢:)请查看我编辑的帖子。在我的例子中,我只想通过使用AVALUE获得DVALUE。当我使用magicstruction时,我不知道b值关于你的:如果你有