AR#检查我的解决方案告诉我"'本地变量'fs'从未使用'"关于这一行:
var fs = new FormatString();
Run Code Online (Sandbox Code Playgroud)
好吧; 只是摆脱整个shebang,对吧?
相反,R#的行动是删除var声明和赋值,留下:
new FormatString();
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是(懊恼的惊喜),它编译!
但这有什么意义吗?
c# resharper object-construction format-string resharper-8.0
通过转发就地构造,我指的是std :: allocator :: construct和各种emplace方法,例如std :: vector :: emplace_back.我只是发现在C++中转发的就地构造不能(无法?)利用列表初始化语法.结果,似乎人们永远无法就地构建聚合.我只是想确定转发的就地构造是否不支持列表初始化,因此也不支持聚合类型.这是由于语言的限制吗?有人可以提供有关此问题的标准吗?以下是一个例子:
虽然我们可以直接进行就地施工
int(*p)[3] = ...;
new(p) int[3]{1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
我们不能做就地转发就像
std::allocator<int[3]> allo;
allo.construct(p, 1, 2, 3);
Run Code Online (Sandbox Code Playgroud) c++ object-construction language-lawyer c++11 list-initialization
我写了一个简单的程序来了解更多关于在C++中创建和销毁对象的顺序(使用Visual Studio 2015).这里是:
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A(string name)
: name(name)
{
cout << "A(" << name << ")::constructor()" << endl;
}
~A()
{
cout << "A(" << name << ")::destructor()" << endl;
}
private:
string name;
};
class C
{
public:
C(string name, A a)
: name(name), a(a)
{
cout << "C(" << name << ")::constructor()" << endl;
}
~C()
{
cout << "C(" << name << ")::destructor()" << endl;
}
private: …Run Code Online (Sandbox Code Playgroud) c++ object-construction temporary-objects object-destruction
我多次发现,当在类中将 std::map 声明为静态内联(C++ 17)时,
struct MyStruct
{
static inline std::map <A, B> mymap;
MyStruct(A& a, B& b)
{
mymap[a] = b;
}
};
Run Code Online (Sandbox Code Playgroud)
如果较早调用 MyStruct 构造函数,即在 main 之前、在第一次使用映射成员时调用,它将会崩溃。
如果 std::map 以不同的方式声明,即
struct MyStruct
{
static std::map <A, B>& mymap()
{
static std::map <A, B> map;
return map;
}
MyStruct(A& a, B& b)
{
mymap()[a] = b;
}
};
Run Code Online (Sandbox Code Playgroud)
那么就不会发生崩溃。
我本以为在这两种情况下,都会在允许继续调用 MyStruct 构造函数之前初始化映射。
谁能解释这里发生了什么?