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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:19:37+00:00 2026-06-17T09:19:37+00:00

Consider a class that just wraps a value at runtime : template <typename Type>

  • 0

Consider a class that just wraps a value at runtime :

template <typename Type>
class NonConstValue 
{
    public:
        NonConstValue(const Type& val) : _value(val) {;}
        Type get() const {return _value;}
        void set(const Type& val) const {_value = val;}
    protected:
        Type _value;
};

and the constexpr version of that:

template <typename Type>
class ConstValue 
{
    public:
        constexpr ConstValue(const Type& val) : _value(val) {;}
        constexpr Type get() const {return _value;}
    protected:
        const Type _value;
};

Question 1 : Can you confirm that the constexpr version is designed in the right way ?

Question 2 : How do you mix both classes into a single one called Value that can be constexpr constructed or runtime constructed and whose value can be get() at runtime or compile-time ?

EDIT :
Question 3 : If get() is defined in a .cpp file, and if I want get() to be inlined if it’s not a constexpr what is the right declaration of the function ? Is it

constexpr inline Type get();

or

inline constexpr Type get()

or something else ?

  • 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-17T09:19:37+00:00Added an answer on June 17, 2026 at 9:19 am

    Just add the constexpr specifier to each of those functions that are potential constant expressions.

    template <typename Type>
    class Value 
    {
    public:
        constexpr Value(Type const& val) : _value(val) {}
        constexpr Type const& get() const {return _value;}
        void set(Type const& val) {_value = val;}
    protected:
        Type _value;
    };
    

    You don’t need a const and a non-const version, since that can be done by instantiating the template Value with a const or non-const type.

    You don’t need a constexpr and a non-constexpr version, constexpr means potential constant expression and whether the expression ends up being a constant expression or not depends on its arguments. Whether or not the expression ends up being evaluated at compile-time depends on the context and the implementation.

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

Sidebar

Related Questions

Lets consider a simple class template< typename T > class Wrapper { public: //
Consider following class of custom array template <typename element, unsigned int size> class array
Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
It may not be so obvious how respond_to? works in ruby. Consider that: class
Consider: public class HomeController : Controller { private IDependency dependency; public HomeController(IDependency dependency) {
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Consider the following: class MyClass { public: int operator ()(int a, int b); };
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following class public class Class1 { public int A { get; set;

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.