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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:50:59+00:00 2026-06-18T20:50:59+00:00

How do I reliably static_assert on anything that isn’t a string literal? For example,

  • 0

How do I reliably static_assert on anything that isn’t a string literal?

For example, in the following code, I’ve attempted to wrap the standard assert macro but statically reject anything for the message that’s not a string literal (since anything but a string literal will not be displayed at runtime when the assert triggers).

#include <cassert>
#include <string>
#include <type_traits>

#define my_assert(test, message)\
    static_assert(\
        (\
            !std::is_pointer<decltype(message)>::value &&\
            !std::is_array<decltype(message)>::value\
        ),\
        "literal string required"\
    );\
    assert((message, (test)));

int main() {
    my_assert(1 == 1, "one equals one");
    my_assert(1 == 2, "one equals two");

    {
        const char *msg = "one equals one";
        //my_assert(1 == 1, msg); // triggers static_assert
    }

    {
        const char msg[] = "one equals one";
        //my_assert(1 == 1, msg); // triggers static_assert
    }

    {
        const std::string msg = "one equals one";
        //my_assert(1 == 1, msg.c_str()); // triggers static_assert
    }

    {
        const int msg = 3;
        my_assert(1 == 1, msg); // should trigger static_assert
    }
}

As you can see, the testing is done via the tests provided by the type_traits header, and,
mostly, this code works as intended (tested with gcc 4.7.2). However, it doesn’t specifically look for string literals as much as it just rejects common things that a programmer might use in place.

The solution I have may be good enough for the example above, but I’d like to use this, or a similar technique in other situations as well.

So the question is, how do I reliably use type_traits (or another standard mechanism) to static_assert on anything except a string literal?

  • 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-18T20:51:01+00:00Added an answer on June 18, 2026 at 8:51 pm

    Here is the best I could get, which appears to reject anything I throw at it, but still accepts literal strings:

    #define my_assert(test, message)\
        static_assert(\
            (\
                 std::is_convertible      <decltype(message), const char *>::value &&\
                !std::is_rvalue_reference <decltype(message)>::value &&\
                !std::is_pointer          <decltype(message)>::value &&\
                !std::is_array            <decltype(message)>::value &&\
                !std::is_class            <decltype(message)>::value\
            ),\
            "string literal required"\
        );\
        assert((message, (test)))
    

    I’d be very interested to know if this actually is exhaustively correct, and/or if there is a simpler way to do this detection.

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

Sidebar

Related Questions

The following code fails to create a new record reliably. TransferRecord transfer = new
How could I reliably detect, deep in my code, whether the current invocation of
I've got a string that has curly quotes in it. I'd like to replace
I've got code that looks like this because the only reliable way for me
I have a bit of code that is supposed to check whether a directory
Suppose I have this code: static void Main(string[] args) { var thread = new
I use this code to get the available IPv4 addresses: static void Main(string[] args)
How do I reliably get the size of a C-style array? The method often
Possible Duplicate: Sending large files reliably in PHP I will be distributing a PHP
Is there any way to reliably capture all changes made to the win32 console

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.