- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There appears to be a bug with inline variable initialization. The following code prints "0". With gcc, it prints "1".
(ICC) 19.0.0.117 20180804
#include <iostream>
int flag = 0;
class C
{
public:
C() { ::flag = 1; }
};
class D
{
public:
static inline C c = C();
};
int main(int, char **)
{
D d;
std::cout << ::flag << std::endl;
}
- Tags:
- CC++
- Development Tools
- Intel® C++ Compiler
- Intel® Parallel Studio XE
- Intel® System Studio
- Optimization
- Parallel Computing
- Vectorization
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This may be a case of vendor dependent behavior between the order of a static constructor and global variable initialization.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, but the behavior is the same if you assert() or throw() in the constructor. The constructor does not get called at all...ever.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Those may be determined at compile time.
Copy the last statement of main, and insert into first statement of the ctor and see what happens.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Strange. If you put the iostream operation in there, it gets run, but if you put throw(0) in the constructor, it doesn't get run...using icpc.
class C
{
public:
C() { throw(0); }
};
class D
{
public:
static inline C c = C();
};
int main(int, char **)
{
D d;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
More to the point, this core dumps with a floating point exception, because the unordered_map ctor doesn't get run. This is how I found the problem:
#include <unordered_map>
class C
{
public:
C() { m.find(0); }
static inline std::unordered_map<int,int> m;
};
int main(int, char **)
{
C c;
return 0;
}

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page