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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:46:01+00:00 2026-06-11T22:46:01+00:00

Consequently to a previous question about const/non-const with ternary operator, is the following test

  • 0

Consequently to a previous question about const/non-const with ternary operator, is the following test function ok regarding to the C++11 standard :

template<bool UseConst> class MyClass
{
    public:
        constexpr bool test()
        {
             return (UseConst) ? (_constvar) : (_var);
        }

    protected:
        int _var;
        static const int _constvar;
}

The whole problem, is that _constvar is const, and _var is non-const. I would have to access these 2 data depending on the template parameter through the same function, and I would like to have a compile-time function when I use const.

Do the test() function satisfy my requirements ?

  • 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-11T22:46:02+00:00Added an answer on June 11, 2026 at 10:46 pm

    You could use SFINAE in order to “specialize” your test function. In other words, you could do something like the following:

    template<bool true_false>
    struct true_type
    {
        static char value;
    };
    
    template<>
    struct true_type<false>
    {
        static char value[2];
    };
    
    template<bool UseConst> class MyClass
    {
        private:
            constexpr int pre_test(const char arg) { return _constvar; }
            int pre_test(char (&)[2]) const { return _var; }
    
        public:
            constexpr int test()
            {
                 return pre_test(true_type<UseConst>::value);
            }
    
        protected:
            int _var;
            static const int _constvar;
    };
    

    Now, when you call MyClass::test, if UseConst is false, test will degrade to a run-time function, but when UseConst is true, you will get a compile-time function.

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

Sidebar

Related Questions

I have a template class A, and a stream operator function for A that
I posted recently a question about the memory overhead due to virtuality in C++.
In this previous question I posted most of my own shell code. My next
In this earlier question the OP asked the following problem: Given a rectangular grid
I am writing a Java application which can encrypt and consequently decrypt whatever binary
I have a fairly complex data model and consequently a fairly complex query (using
So, I'm pretty new to the whole GitHub thing. Consequently my repo looks like
How hard could It be to have the Tex engine and consequently latex compiling
I have two tables registries and names . Consequently, I have two model classes.
I have a class ( MyClass ) with a lot of methods. Consequently, the

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.