dar*_*une 6 c++ c++17 visual-studio-2017
我看到一些inline const变量被Visual Studio 2017初始化(和破坏)3次的例子.这是链接器的错误吗?或者这应该以其他方式发生?
链接器Comdat折叠设置为关闭.
示例代码:
#pragma once
struct A {
A() {
static int count = 0;
++count;
ASSERT(count == 1);
}
~A() {
}
};
inline const A a = A();
Run Code Online (Sandbox Code Playgroud)
在我的解决方案中,我有两次断言(一个构造函数被称为3次).检查调用堆栈显示所有调用堆栈都是相同的,并且所有调用都来自()的动态初始化程序.现在我知道这个类没有用在解决方案的其他部分,因为我刚创建它来调查这个问题.
我使用VS17 15.8.9
更新:此处的错误报告https://developercommunity.visualstudio.com/content/problem/297876/static-inline-variable-gets-destroyed-multiple-tim.html(您可以通过upvote来帮助推送错误修正)
这似乎是一个MSVC错误.我可以使用下面的代码重现它(也使用VS2017 15.8.9).有趣的是,我只能使用Debug构建重现.在发布模式下,优化器似乎可以保存我们.
COMMON.H
#pragma once
#include <iostream>
class Foo
{
public:
Foo()
{
std::cout << "Constructing a Foo" << std::endl;
}
~Foo()
{
std::cout << "Destructing a Foo" << std::endl;
}
};
inline Foo const Bar;
Run Code Online (Sandbox Code Playgroud)
other.cpp
#include "common.h"
void DoOtherStuff()
{
std::cout << &Bar << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "common.h"
void DoStuff()
{
std::cout << &Bar << std::endl;
}
extern void DoOtherStuff();
int main()
{
DoStuff();
DoOtherStuff();
}
Run Code Online (Sandbox Code Playgroud)
输出(调试)
Constructing a Foo
Constructing a Foo
00007FF74FD50170
00007FF74FD50170
Destructing a Foo
Destructing a Foo
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
446 次 |
| 最近记录: |