Following is an minimal example that sometimes generates guards and sometimes not:
struct A {
inline A(int v = 0) {} // without ctors, guards are omitted
int m1() const {
return m;
}
private:
int m = 0;
};
//namespace { // without anon-ns guards are generated
template<typename T>
struct X {
static int foo() {
// static T m; // even as local-static and -fno-threadsafe-statics, guards are generated
return m.m1();
}
inline static T m; // comment this and uncomment above to try as local-static
};
//}
int main() {
return X<A>::foo();
}
To summarize:
- without ctor in class A, guards are never generated
- using anaon-ns prevents guards also
- making the static member
ma static local infoo()still generates guards (with-fno-threadsafe-statics) (comment/uncomment the appropriate lines in example above)
So, how to grevent guards from being generated in the case class A has a ctor and using anon-ns is not possible?