26

Consider this example code:

template<class D>
char register_(){
    return D::get_dummy(); // static function
}

template<class D>
struct Foo{
    static char const dummy;
};

template<class D>
char const Foo<D>::dummy = register_<D>();

struct Bar
    : Foo<Bar>
{
    static char const get_dummy() { return 42; }
};

(Also on Ideone.)

I'd expect dummy to get initialized as soon as there is a concrete instantiation of Foo, which I have with Bar. This question (and the standard quote at the end) explained pretty clear, why that's not happening.

[...] in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

Is there any way to force dummy to be initialized (effectively calling register_) without any instance of Bar or Foo (no instances, so no constructor trickery) and without the user of Foo needing to explicitly state the member in some way? Extra cookies for not needing the derived class to do anything.


Edit: Found a way with minimal impact on the derived class:

struct Bar
    : Foo<Bar>
{   //                              vvvvvvvvvvvv
    static char const get_dummy() { (void)dummy; return 42; }
};

Though, I'd still like the derived class not having to do that. :|

13
  • 1
    @Bo: Sure, but I'd like to hide that use from the derived class / outer world and would rather somehow get that into Foo itself. :/
    – Xeo
    Jun 21, 2011 at 6:35
  • 1
    @Serge: Why shouldn't it? The dummy in get_dummy is the uninitialized one, sure, but that doesn't matter. :) I don't actually use it anyways.
    – Xeo
    Jun 21, 2011 at 10:06
  • 1
    @Xeo: "The dummy in get_dummy is the uninitialized one, sure, but that doesn't matter. :) I don't actually use it anyways." Yea. But why it is supposed to force Foo<Bar>::dummy to become initialized? And what is supposed to prevent compiler from optimizing statement (void)dummy; (that has no effect) out. Jun 21, 2011 at 10:21
  • 1
    @Serge: It forces dummy to become initialized, because it is used, as quoted. w.r.t. optimizations, I'll have to check that.
    – Xeo
    Jun 21, 2011 at 10:57
  • 1
    @Xeo: "It forces dummy to become initialized, because it is used" Not actually. It is used only if you call get_dummy() somewhere. Static members and global variables are initialized either before first statement of main or before first using of any object or function defined in the same translation unit as that static member or global variable. So it is totally allowed to not initialize Foo<Bar>::dummy at all if you don't use any objects or functions from the same translation unit where template Foo specialization is instantiated. Jun 21, 2011 at 17:11

5 Answers 5

12

Consider:

template<typename T, T> struct value { };

template<typename T>
struct HasStatics {
  static int a; // we force this to be initialized
  typedef value<int&, a> value_user;
};

template<typename T>
int HasStatics<T>::a = /* whatever side-effect you want */ 0;

It's also possible without introducing any member:

template<typename T, T> struct var { enum { value }; };
typedef char user;

template<typename T>
struct HasStatics {
  static int a; // we force this to be initialized
  static int b; // and this

  // hope you like the syntax!
  user :var<int&, a>::value,
       :var<int&, b>::value;
};

template<typename T>
int HasStatics<T>::a = /* whatever side-effect you want */ 0;

template<typename T>
int HasStatics<T>::b = /* whatever side-effect you want */ 0;
4
  • The first one seems to work on GCC, but is no good in MSVC10, no initialization happens. :/ The second one works on neither GCC nor MSVC10. :( Edit: Oh wait, with C++0x enabled it works on GCC. Still no luck with MSVC...
    – Xeo
    Jun 23, 2011 at 20:52
  • @Xeo it works for me without c++0x on gcc4.6. Don't have any other compiler to try, except clang. On clang the second one doesn't work either. PR is on the way. Jun 23, 2011 at 20:57
  • 5
    Oh Gosh! what user :var<int&, a>::value does mean? Jun 23, 2011 at 21:00
  • 4
    @pure: It's an unnamed bitfield, effectively a char : 0;
    – Xeo
    Jun 23, 2011 at 21:07
5

We can use a simple trick based on a declaration that must be instantiated with the class:

template<…>
struct Auto {
  static Foo foo;
  static_assert(&foo);
};
template<…> Foo Auto::foo=…;

Note that some compilers warn about the comparison to null; that can be avoided with &foo==&foo, (bool)&foo, or ((void)&foo,true) if needed.

Note also that GCC 9.0–9.2 don’t count this as an odr-use.

2
  • The most elegant option so far, thanks. I am a little worried I am calling this "elegant", but hey, this is C++ :)
    – Mikhail
    Mar 15, 2021 at 10:25
  • Thank, but doesn't work on Visual C++ 2019. Sound like Visual C++ bug. Mar 2 at 1:42
0

Something like that comes to my mind:

// in some c++ file (to make i with internal linkage)
static int i = init_dummy(Foo<int>::dummy);

where init_dummy is defined like this:

int init_dummy(...)
{
  return 1;
}

Due to variable args you can put more initializations there like:

static int i = init_dummy(Foo<int>::dummy, Foo<double>::dummy, Foo<whatever>::dummy);
0

I recently bumped into this. The official solution would be explicit template instantiation, described here.

In the above case, the statement should look like this:

template class Foo::Bar;
-2

Is there any way to force dummy to be initialized (effectively calling register_) without any instance of Bar or Foo (no instances, so no constructor trickery)?

Wouldn't this be sufficient?

std::cout << Foo<int>::dummy;
1
  • I hope there are other ways than having the user to explicitly state the member. :|
    – Xeo
    Jun 21, 2011 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.