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

  • SEARCH
  • Home
  • 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 728929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:44:17+00:00 2026-05-14T06:44:17+00:00

I’m writing some code which could really do with some simple compile time metaprogramming.

  • 0

I’m writing some code which could really do with some simple compile time metaprogramming. It is common practise to use empty-struct tags as compile time symbols. I need to decorate the tags with some run-time config elements. static variables seem the only way to go (to enable meta-programming), however static variables require global declarations. to side step this Scott Myers suggestion (from the third edition of Effective C++), about sequencing the initialization of static variables by declaring them inside a function instead of as class variables, came to mind.

So I came up with the following code, my hypothesis is that it will let me have a compile-time symbol with string literals use-able at runtime. I’m not missing anything I hope, and that this will work correctly, as long as I populate the runtime fields before I Initialize the depending templates classes ? .

#include <string>

template<class Instance>

class TheBestThing {
public:
   static void set_name(const char * name_in) {
      get_name() = std::string(name_in);
   }
   static void set_fs_location(const char * fs_location_in) {
      get_fs_location() = std::string(fs_location_in);
   }
   static std::string & get_fs_location() {
      static std::string fs_location;
      return fs_location;
   }
   static std::string & get_name() {
      static std::string name;
      return name;
   }  
};
struct tag {};
typedef TheBestThing<tag> tbt;

int main()
{
   tbt::set_name("xyz");
   tbt::set_fs_location("/etc/lala");

   ImportantObject<tbt> SinceSlicedBread;
}

edit:
Made community wiki.

  • 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-14T06:44:17+00:00Added an answer on May 14, 2026 at 6:44 am

    I’ve finally understood what the problem was… and your solution does not solve much, if any.

    The goal of using local static variable is to provide initialization on first use, thus being safe from the “Initialization Order Fiasco” (by the way, it does not solve the “Destruction Order Fiasco”).

    But with your design, if you effectively prevent the crash you do not however prevent the issue of using a variable before its value is used.

    ImportantObject<tbt> SinceSliceBread; // using an empty string
    
    tbt::set_name("xyz");
    

    Compare with the following use:

    std::string& tbt::get_name() { static std::string MName = "xyz"; return MName; }
    

    Here the name is not only created but also initialized on first use. What’s the point of using a non initialized name ?

    Well, now that we know your solution does not work, let’s think a bit. In fact we would like to automate this:

    struct tag
    {
      static const std::string& get_name();
      static const std::string& get_fs_location();
    };
    

    (with possibly some accessors to modify them)

    My first (and easy) solution would be to use a macro (bouh not typesafe):

    #define DEFINE_NEW_TAG(Tag_, Name_, FsLocation_)              \
      struct Tag_                                                 \
      {                                                           \
        static const std::string& get_name() {                    \
          static const std::string name = #Name_;                 \
          return name;                                            \
        }                                                         \
        static const std::string& get_fs_location() {             \
          static const std::string fs_location = #FsLocation_;    \
          return fs_location;                                     \
        }                                                         \
      };
    

    The other solution, in your case, could be to use boost::optional to detect that the value has not been initialized yet, and postpone initialization of the values that depend on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 410k
  • Answers 410k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As you suggest the best way would probably be to… May 15, 2026 at 7:22 am
  • Editorial Team
    Editorial Team added an answer Solution is to issue the following query: (|(&(objectclass=computer)(memberof=))(&(objectclass=computer)(primarygroupid={1}))) The first… May 15, 2026 at 7:22 am
  • Editorial Team
    Editorial Team added an answer There are two (or more) possible ways. One is to… May 15, 2026 at 7:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.