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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:14:05+00:00 2026-06-07T10:14:05+00:00

Is there a way to force derived types to define a static constexpr in

  • 0

Is there a way to force derived types to define a static constexpr in C++? I have a base class, and I want to force every derived class to define a static const bool has_property.

I tried doing this with CRTP (so that each derived class gets its own static const):

template <typename T, class MyClass>
struct Base {
    T data;
    static constexpr bool has_property;
};

template <typename T>
struct Derived : public Base<T, Derived<T> > {
    static constexpr bool has_property = false;
};

But the compiler complains that Base::has_property is not initialized.

How can I accomplish this?

  • 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-07T10:14:07+00:00Added an answer on June 7, 2026 at 10:14 am

    We could insert a static_assert into the constructor of Base:

    template <typename T, class MyClass>
    struct Base {
        T data;
    
        Base() {
            static_assert(std::is_same<
                            typename std::decay<
                              decltype(MyClass::has_property)
                            >::type, bool
                          >::value, 
                          "Must be a bool");
            static_assert(MyClass::has_property || true, 
                          "Must be constexpr");
        }
    };
    

    This check will only work when the derived class is instantiated, and the default constructor of Base is used. The constexpr check doesn’t work in g++ 4.7 at the moment.


    Alternatively, you could use a type traits instead of a constexpr static member, e.g.

    template <typename T>
    struct has_property;
    
    template <typename T>
    struct has_property<Derived<T>> : std::true_type {};
    
    // use as: has_property<X>::value.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to force classes in Java to have public static final
Can I somehow force a derived class to always call the overridden methods base?
I have a base class that I want to look like this: class B
Is there a way to force C# compiler, visual studio 2010, to call a
Is there a way to force VS to use Unicode always, instead of weird
is there some way to force the gridview's pager to show up, even when
Is there a way to force the client to update its local svn configuration
Is there any way to force a Route to be executed, only if all
Is there any way to force a browser to refresh or empty it's cache,
Is there any way to force wpf to run the value converter again on

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.