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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:33:12+00:00 2026-06-14T13:33:12+00:00

Is there a way to print the value of a constexpr or #define d

  • 0

Is there a way to print the value of a constexpr or #defined value at compile time? I want the equivalent of std::cout <<, or some way to do something like

constexpr int PI_INT = 4;
static_assert(PI_INT == 3,
              const_str_join("PI_INT must be 3, not ", const_int_to_str(PI_INT)));

Edit: I can do some basic compile-time printing with constexprs, at least on gcc by doing something like

template <int v>
struct display_non_zero_int_value;

template <>
struct display_non_zero_int_value<0> { static constexpr bool foo = true; };

static constexpr int v = 1;

static_assert(v == 0 && display_non_zero_int_value<v>::foo, "v == 0");

which gives me error: incomplete type ‘display_non_zero_int_value<1>’ used in nested name specifier static_assert(v == 0 && display_non_zero_int_value<v>::foo, "v == 0");. (icpc, on the other hand, is less helpful, and just says error: incomplete type is not allowed) Is there a way to write a macro that can generalize this so that I can do something like

constexpr int PI_INT = 4;
PRINT_VALUE(PI_INT)

and get an error message that involves 4, somehow?

  • 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-14T13:33:13+00:00Added an answer on June 14, 2026 at 1:33 pm

    Quoting the grammar given for declarations in §7/1 [dcl.dcl]:

    static_assert-declaration:

    static_assert ( constant-expression , string-literal ) ;

    The standard says it has to be a string literal, so you’re out of luck; you can’t use a constexpr function to construct your error message.

    You can, however, use whatever preprocessor magic you like to generate a string literal to go in there. If PI_INT is a #define instead of a constexpr int, you could use something like this:

    #define PI_INT 4
    #define pi_err_str_(x) #x
    #define pi_err_str(x) pi_err_str_(x)
    #define pi_int_err "PI_INT must be 3, not " pi_err_str(PI_INT)
    
    static_assert(PI_INT == 3, pi_int_err);
    

    output:

    error: static assertion failed: "PI_INT must be 3, not 4"


    Edit in response to comment by OP and updated question

    Is there a way to write a macro that can generalize this so that I can do something like … and get an error message that involves 4, somehow?

    Sure, a bit of preprocessor magic can generalize that, assuming you’re happy to be reliant on compiler-specific error message behaviour:

    #define strcat_(x, y) x ## y
    #define strcat(x, y) strcat_(x, y)
    #define PRINT_VALUE(x) template <int> struct strcat(strcat(value_of_, x), _is); static_assert(strcat(strcat(value_of_, x), _is)<x>::x, "");
    
    constexpr int PI_INT = 4;
    PRINT_VALUE(PI_INT)
    

    stackoverflow/13465334.cpp:20:1: error: incomplete type ‘value_of_PI_INT_is<4>’ used in nested name specifier

    As for other compilers, I don’t know what you can do offhand, but you may want to look at a copy of boost’s static_assert.hpp to see if any of the tricks employed there can be used to get an evaluated template arg printed.

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

Sidebar

Related Questions

Is there a way to print both key and value of a anonymous dict
Is there a way to make Python's optparse print the default value of an
Is there a way to print, via the SDK (I'm using qbXML and the
Is there a way to print the number of keys in Redis? I am
In a replacement pattern, is there any way to print the NUMBER of the
Is there's a way to print the variable in the element as an attribute?
Is there really no way to print an ascii string in assembly to standard
Is there a way to only print or output 10 elements per row inside
Is there any way I can print/show images on top of each other. The
Is there an easy way to print the IP_Address:port# ? Because I soon as

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.