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 8730581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:10+00:00 2026-06-13T09:03:10+00:00

Context I oftenly use UUID implementation of Boost library to identify derived classes. To

  • 0

Context

I oftenly use UUID implementation of Boost library to identify derived classes.
To do so I usually use the following :
In the declaration file :

  #include "ClassA.h"
  #include <boost/uuid/uuid.hpp>

  class SubClass1 : public ClassA {
    public:
      static const boost::uuids::uuid classId; // 7feb24af-fc38-44de-bc38-04defc3804de
    ... 
  };

In the implementation file :

  #include "SubClass1.h"
  #include <boost/uuids/uuid_generator.h>

  const boost::uuids::uuid SubClass1 ::classId = boost::uuids::string_generator()("{7feb24af-fc38-44de-bc38-04defc3804de}");
  ...

Question

I would like to know if it is possible to assign a value to the UUID in the declaration file.

Ideas

At first, I thought it was possible because Boost implementation is POD. Therefore I tried several ways to assign a value directly in the header using aggregate initializers (see boost documentation for an example of non static aggregate initializers) :

  static const boost::uuids::uuid classId = { 0x7f, 0xeb, ... };

Unfortunately, it failed at compilation (the compiler can only initialize static const integral type).

Have you any suggestions to solve this issue, preferably using the Boost implementation of UUIDs ?

  • 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-06-13T09:03:11+00:00Added an answer on June 13, 2026 at 9:03 am

    The easiest way to define a non-integral constant as a class member in a header file is to wrap it in a function, like this:

    typedef whatever Uuid;
    
    class MyClass
    {
    public:
        static Uuid const& uuid()
        {
            Uuid const theValue = ...;
            return theValue;
        }
    };
    

    If you want it as an actual constant, not a function, then you can use a little templating trick, like so:

    template< class Dummy >
    class MyClass_constants
    {
    public:
        static Uuid const uuid;
    };
    
    template< class Dummy >
    Uuid const MyClass_constants<Dummy>::uuid = ...;
    
    class MyClass
        : public MyClass_constants<void>
    {};
    

    With C++11 you can use constexpr, like this:

    class MyClass
    {
    public:
        static Uuid constexpr uuid = ...;
    };
    

    but, although a test of that compiles fine with no linker warnings with g++ 4.7.1, it’s so new that I’m not sure if the standard’s One Definition Rule actually supports it, or whether, with such definition in multiple translation units, I’m into Undefined Behavior.

    So, if you want to do the constexpr, perhaps ask a separate question about the ODR.

    As an alternative you can wait for the C++2040 standard and then just write

    inline static Uuid const uuid = ...;
    

    It is food for thought that, as shown, this hypothetical future feature can be emulated even in C++98, i.e. that all that’s needed is already implemented by compiler and linker, and has been since the first standard. And yet, that that simple feature is lacking in the language. I believe that it’s due to a kind of haphazard historical language evolution.

    Anyway, I’d go for the simple function wrapper shown first. 🙂

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

Sidebar

Related Questions

Context I use FINDSTR /C:portid= scanports.xml to extract theses lines from a file: <port
Context: I have an application that makes heavy use of the GSL library and
Context I am aware of http://closure-library.googlecode.com/svn/trunk/closure/goog/demos/editor/equationeditor.html# However, when I try it, it does not
Context I am trying to understand the internals of a big, hairy JavaScript library.
Context I'm currently reading about Clojure's implementation of monads: org.clojure/algo.monads Intuitively, reduce looks like
Context I want to use JavaFx with clojure. I am aware of http://nailthatbug.net/2011/06/clojure-javafx-2-0-simple-app/ Question:
Context I use jQuery UI autocomplete with a remote datasource. The source send data
Context I have a very large number of small classes or structures . The
Context: I want to use log4j to write audit-related logs to a specific log
Context The following code produces a nice looking Hello World graphics.drawString(Hello World, 30, 30);

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.