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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:19:51+00:00 2026-06-09T16:19:51+00:00

In the following piece of code, what function will allow the best optimization for

  • 0

In the following piece of code, what function will allow the best optimization for an external use and why ? Is the “Version 4” allowed in C++ 2011 ?

template<unsigned int TDIM> class MyClass 
{
    public:
        static inline unsigned int size()           {return _size;} // Version 1
        static inline const unsigned int size()     {return _size;} // Version 2
        static constexpr unsigned int size()        {return _size;} // Version 3
        static inline constexpr unsigned int size() {return _size;} // Version 4
    protected:
        static const unsigned int _size = TDIM*3;
};

Thank you very much.

  • 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-09T16:19:53+00:00Added an answer on June 9, 2026 at 4:19 pm

    I believe that the code in <random> sets a good example, but also need not be followed slavishly. In <random> you see both of these styles:

    template<unsigned int TDIM> class MyClass 
    {
        public:
            static constexpr unsigned int size() {return _size;}  // 1
            static constexpr unsigned int dim = TDIM;             // 2
        private:
            static const unsigned int _size = TDIM*3;
    };
    

    The choice between 1 and 2 is largely stylistic. They are both resolved at compile time when used in a way that demands a compile-time result. Do you want your clients to type () or not? Is there generic code that will need to use one style or the other? Satisfying the requirements of generic code is key here.

    Use of the inline keyword has no impact here. I consider it overly verbose, but it does no harm and has no impact if you use it.

    Adding const to a return type will have no impact here. I consider it overly verbose, but it does no harm and has no impact if you use it.

    If you use the function style, but do not use constexpr:

        static unsigned int size() {return _size;}
    

    then this function can not be called at compile-time, and thus can not be used in a context which expects a compile-time constant. That may not cause any harm for your application or your clients if they don’t need such functionality. But imho, if you’ve got constexpr in the toolbox, this is the perfect place to use it. If you do a future client can do stuff like this:

    template <unsigned N> struct B {};
    constexpr auto myclass = MyClass<3>();
    // ...
    // lots of code here
    // ...
    B<myclass.size()> b;
    

    These two are equivalent:

            static constexpr unsigned int dim = TDIM;        // 2
            static const unsigned int dim = TDIM;            // 3
    

    but only because the involved type is integral. If the type is not integral, then you have to use constexpr and the type has to have a constexpr constructor:

    class A
    {
        unsigned _i;
    public:
        constexpr A(unsigned i) : _i(i) {}
    };
    
    template<unsigned int TDIM> class MyClass 
    {
        public:
            static constexpr unsigned int size() {return _size;}
            static constexpr unsigned int dim = TDIM;
            static constexpr A a = A(dim);
        private:
            static const unsigned int _size = TDIM*3;
    };
    

    Everyone here, including myself, is still learning how to use constexpr. So +1 on the question.

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

Sidebar

Related Questions

I have the following piece of code from a function that takes the host
I have the following piece of code in my Model: public function getSite() {
I have the following piece of code from a book. There is this function
I have the following piece of code: $(document).ready(initialize); function initialize() { $('#btnPoint').css('width', $('#btnPoint').width()); }
I created the following function to simplify a piece of particularly complex code. CREATE
the following piece of code works fine when i use it among some code
I just written the following piece of code template<typename T> inline bool contains(T haystack,typename
See the following piece of simplified code: myVar= 1; myFunc = function() { return
I have following piece of code: $(document).ready(function(){ $(#cont).hide(); $(#slide).show(); $('#slide').click(function(){ $(#cont).slideToggle(); $(#slide).css(background-image, url('img/slideup.png')); });
i have the following piece of code in the page HEAD: <script type=text/javascript> $(document).ready(function(){

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.