Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:55:19+00:00 2026-05-23T10:55:19+00:00

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

  • 0

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. 😐

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:55:19+00:00Added an answer on May 23, 2026 at 10:55 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please consider the following code: template <typename T> struct foo { template <typename S>
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
Consider the following example code: class Foo { }; class Bar : public Foo
Consider this example code: a={}; a['herp']=['derp']; var b = jQuery.extend({}, a); b['herp'].push('foo'); alert(a['herp']); //this
Consider this example Javascript code: a = new Array(); a['a1']='foo'; a['a2']='bar'; b = new
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Consider this code example: var source = null; $.ajax({ [...], success:function(data) { source =
Consider this code: function klass( z ) { this.a = z; return this; }
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.