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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:42:06+00:00 2026-06-05T13:42:06+00:00

I got this compile time string comparison from another thread using constexpr and C++11

  • 0

I got this compile time string comparison from another thread using constexpr and C++11 (http://stackoverflow.com/questions/5721813/compile-time-assert-for-string-equality). It works with constant strings like “OK”

    constexpr bool isequal(char const *one, char const *two) {
        return (*one && *two) ? (*one == *two && isequal(one + 1, two + 1))
        : (!*one && !*two);
    }

I am trying to use it in the following context:

 static_assert(isequal(boost::mpl::c_str<boost::mpl::string<'ak'>>::value, "ak"), "should not fail");

But it gives me an compilation error of static_assert expression is not an constant integral expression.

Can I do 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-05T13:42:08+00:00Added an answer on June 5, 2026 at 1:42 pm

    The problem is that the value member of mpl::c_str is not marked as constexpr. Until the library authors decide to include support for constexpr, you are pretty much screwed, unless you are willing to modify your Boost code (or create your own version of c_str). If you decide to do so, the modification is quite simple: you just need to locate BOOST_ROOT/boost/mpl/string.hpp and replace this

    template<typename Sequence>
    struct c_str
    {
        ...
        static typename Sequence::value_type const value[BOOST_MPL_LIMIT_STRING_SIZE+1] 
    };
    
    template<typename Sequence>
    typename Sequence::value_type const c_str<Sequence>::value[BOOST_MPL_LIMIT_STRING_SIZE+1] =
    {
        #define M0(z, n, data)                                                                      \
        mpl::aux_::deref_unless<BOOST_PP_CAT(i, n), iend>::type::value,
        BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
        #undef M0
        '\0'
    };
    

    by this

    template<typename Sequence>
    struct c_str
    {
        ...
        static constexpr typename Sequence::value_type value[BOOST_MPL_LIMIT_STRING_SIZE+1] =
        {
            #define M0(z, n, data)                                                                      \
            mpl::aux_::deref_unless<BOOST_PP_CAT(i, n), iend>::type::value,
            BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
            #undef M0
            '\0'
        };
    };
    
    // definition still needed
    template<typename Sequence>
    constexpr typename Sequence::value_type c_str<Sequence>::value[BOOST_MPL_LIMIT_STRING_SIZE+1];
    

    Hmm, after digging a bit more, it turns out the problem is more complex than I thought. In fact, static constants can be used in constexpr; the true problem is that c_str<T>::value is an array, and your function takes pointers as parameters. As a consequence, the compiler needs to decay the array, which boils down to taking the address of its first element. Since addresses are a runtime concept, it is not possible to take the address of an object in a constexpr.

    To solve the issue, I tried to write a second version of isequal that operates on arrays rather than on pointers:

    template <int N, int M>
    constexpr bool isequal(char const (&one)[N], char const (&two)[M], int index)
    {
        return (one[index] && two[index]) ?
            (one[index] == two[index] && isequal(one, two, index + 1)) :
            (!one[index] && !two[index]);
    }
    
    template <int N, int M>
    constexpr bool isequal(char const (&one)[N], char const (&two)[M])
    {
        // note: we can't check whether N == M since the size of the array
        // can be greater than the actual size of the null-terminated string
        return isequal(one, two, 0);
    }
    
    constexpr char hello[] = "hello";
    static_assert(isequal(hello, hello), "hello == hello");
    constexpr char zello[] = "zello";
    static_assert(!isequal(hello, zello), "hello != zello");
    constexpr char hel[] = "hel";
    static_assert(!isequal(hello, hel), "hello != hel");
    

    Unfortunately, this code does not work with mpl::c_str; in fact, the problem is that static const arrays are not compile-time values, unlike integral constants. So we’re back the beginning: unless value is marked as constexpr, there is no way to use it in a constant expression.

    As to why the code I gave initially fails, I can’t answer right now as my version of gcc (4.6) fails to compile it altogether…

    After updating gcc, it turns out value needs to be defined outside the class, even though it is declared and initialized in the class (see this question). I edited the code above with the correction.

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

Sidebar

Related Questions

I've got a compile-time recursive type that looks somewhat like this: template <typename DataType,
I just got help in how to compile this script a few mintues ago
Got this: http://jsfiddle.net/NdsF8/ Want it to be dynamic so when you click a new
Got this error while trying to get the variables out of a class. Using
Got this as an interview question from Amazon to test basic SQL skills and
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
With reflection, you can look up a class from a string at run time,
I've got a generic method which converts an id from a string (eg, retrieved
I got the code below from a website,and this way of serial port reading
I finally got this calculator application in Netbeans to compile correctly and run client-side

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.