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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:15:19+00:00 2026-06-14T04:15:19+00:00

Is it possible to concat two string literals using a constexpr ? Or put

  • 0

Is it possible to concat two string literals using a constexpr? Or put differently, can one eliminate macros in code like:

#define nl(str) str "\n"

int main()
{
  std::cout <<
      nl("usage: foo")
      nl("print a message")
      ;

  return 0;
}

Update: There is nothing wrong with using "\n", however I would like to know whether one can use constexpr to replace those type of macros.

  • 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-14T04:15:20+00:00Added an answer on June 14, 2026 at 4:15 am
    1. Yes, it is entirely possible to create compile-time constant strings, and manipulate them with constexpr functions and even operators. However,

    2. The compiler is not required to perform constant initialization of any object other than static- and thread-duration objects. In particular, temporary objects (which are not variables, and have something less than automatic storage duration) are not required to be constant initialized, and as far as I know no compiler does that for arrays. See 3.6.2/2-3, which define constant initialization, and 6.7.4 for some more wording with respect to block-level static duration variables. Neither of these apply to temporaries, whose lifetime is defined in 12.2/3 and following.

    So you could achieve the desired compile-time concatenation with:

    static const auto conc = <some clever constexpr thingy>;
    std::cout << conc;
    

    but you can’t make it work with:

    std::cout <<  <some clever constexpr thingy>;
    

    Update:

    But you can make it work with:

    std::cout << *[]()-> const {
                 static constexpr auto s = /* constexpr call */;
                 return &s;}()
              << " some more text";
    

    But the boilerplate punctuation is way too ugly to make it any more than an interesting little hack.


    (Disclaimer: IANALL, although sometimes I like to play one on the internet. So there might be some dusty corners of the standard which contradicts the above.)

    (Despite the disclaimer, and pushed by @DyP, I added some more language-lawyerly citations.)

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

Sidebar

Related Questions

Is it possible to CONCAT values of two table columns using the Yii's ActiveRecord
Can we concat two properties together in binding expression? If possible without converter or
Possible Duplicate: Concat all strings inside a List<string> using LINQ I am using C#
Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat?
Is it possible to put GROUP_CONCAT in a MYSQL WHERE clause? I have two
Possible Duplicate: Concat field value to string in SQL Server What is the SQL
Possible Duplicate: Stringbuilder vs String.Concat As I understand it, String are immutable, so if
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want

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.