使用STD ::函数的匿名类成员变量的初始化
Initialization of anonymous class member variable with std::function
我想知道是否有解决方法:
class A
{
class
{
public:
void setValue(int val) {i=val;}
private:
int i;
} B = initB(10);
std::function<decltype(B)(int)> initB = [this](int value)
{decltype(B) temp;
temp.setValue(value);
return temp;};
}
//...
A a; //crash
//...
我想这是由初始化顺序引起的。可变B
是通过调用未定性的std::function
实例来发起的,因此崩溃。通过我的逻辑,解决方法是先初始化std::function
,然后初始化成员B
。但是,此类代码无效:
class A
{
//error: 'B' was not declared in this scope
std::function<decltype(B)(int)> initB = [this](int value)
{decltype(B) temp;
temp.setValue(value);
return temp;};
class
{
public:
void setValue(int val) {i=val;}
private:
int i;
} B = initB(10);
}
我试图制作std::function
static
,并且此类代码有效,但需要non-constexpr/const
成员,因为std :: function具有 non-trivial destructor
-不好,因为这需要源文件,这需要创建此类文件,这需要一些文件效果和破坏我美丽的仅标题级别的层次结构!(我的意思是,我可能会很懒惰,并在标题中定义了此变量,但随后发生了多个定义问题)。我知道这可能是一个糟糕的设计(我只是在测试内容),但是您是否有任何想法在不涉及源文件的情况下解决问题?
尽管您的示例是人为的,但有时我需要(或更方便)以类似的方式初始化复杂对象。
但是,为什么要使用std :: function&lt;>?为什么不使用功能?
class A
{
class
{
public:
void setValue(int val) { i = val; }
private:
int i;
} B = initB(10);
static decltype(B) initB(int value)
{
decltype(B) temp;
temp.setValue(value);
return temp;
}
};
,尽管我通常不会使用dectType(b);我只会给班级一个名字。
我觉得自己在某种程度上颠覆了您的意图,但是如果您初始化构造函数中的变量,则可以使事物正常工作。
#include <functional>
class A
{
class
{
public:
void setValue(int val) {i=val;}
private:
int i;
} B;
std::function<decltype(B)(int)> initB;
public:
A() {
initB = [this](int value)
{decltype(B) temp;
temp.setValue(value);
return temp;};
B = initB(10);
}
};
int main() {
A a;
}
a :: initb是一个值。它不是在您调用它的点上初始化的,因为在指定会员字段的顺序下完成了初始化(松散说话)。您可以通过执行以下操作来验证这一点:
#include <iostream>
#include <functional>
using namespace std;
template<typename T, typename U>
T set(T& tgt, const U& src)
{
new(&tgt) T(src);
return tgt;
}
class A
{
class
{
public:
void setValue(int val) {i=val;}
private:
int i;
} B = set(initB, [this](int value)
{decltype(B) temp;
temp.setValue(value);
return temp;})(10);
std::function<decltype(B)(int)> initB;
};
int main() {
A a;
}
相关文章:
- C++成员初始化
- c++构造函数成员初始化:传递参数
- C++正确的指针成员初始化
- 将另一个类的对象传递到当前类C++的构造函数中(不是成员初始化)
- WinLamb 错误:成员初始化非法
- 使用其他成员初始化结构的成员?
- C++模板类静态成员初始化
- 解释了构造函数成员初始化列表
- 如何在成员初始化列表中声明共享指针
- C++入门5版:使用get成员初始化另一个与shared_ptr无关的对象
- C++11 默认类成员初始化与初始值设定项列表同时
- 调用非默认构造函数作为成员初始化
- C++模板成员初始化:用右值移动构造,但用左值移动引用
- 类成员初始化C++
- 在成员初始化列表中,我可以创建对列表中不在列表中的成员变量的引用
- C :(不重复)积分静态成员初始化(不仅是声明!),导致链接器错误,原因
- 如何调用成员初始化器列表中参考成员的构造函数
- C 构造函数采用成员初始化器
- 与其他静态const成员初始化静态常量成员
- 静态内联成员初始化顺序