7
#include <iostream>
#include <string>

constexpr std::string appendStringC(std::string s)
{
    return s + " Constexpr func";
}

std::string appendString(std::string s)
{
    return s + " Regular func";
}

int main()
{
    std::string s1 = "String 1";
    std::string s2 = "String 2";
    std::cout << std::endl
              << appendStringC(s1) << std::endl
              << appendString(s2) << std::endl;
    return 0;
}

I'm trying to perform compile time string prefix/postfix concatenation operation using constexpr. However this example is producing following errors:

const_string_generation.cpp: In function ‘constexpr std::__cxx11::string appendStringC(std::__cxx11::string)’:
const_string_generation.cpp:4:23: error: invalid type for parameter 1 of constexpr function ‘constexpr std::__cxx11::string appendStringC(std::__cxx11::string)’
 constexpr std::string appendStringC(std::string s)
                       ^
In file included from /usr/include/c++/5.3.0/string:52:0,
                 from /usr/include/c++/5.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/5.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/5.3.0/ios:42,
                 from /usr/include/c++/5.3.0/ostream:38,
                 from /usr/include/c++/5.3.0/iostream:39,
                 from const_string_generation.cpp:1:
/usr/include/c++/5.3.0/bits/basic_string.h:71:11: note: ‘std::__cxx11::basic_string<char>’ is not literal because:
     class basic_string
           ^
/usr/include/c++/5.3.0/bits/basic_string.h:71:11: note:   ‘std::__cxx11::basic_string<char>’ has a non-trivial destructor
const_string_generation.cpp:4:23: error: invalid return type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ of constexpr function ‘constexpr std::__cxx11::string appendStringC(std::__cxx11::string)’
 constexpr std::string appendStringC(std::string s)

since std::string isn't a literal. I'm looking for an easy way to do this and I don't care about backward compatiblity for this very example. Ideone : Link


Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
  • 4
    You can't, since `std::string` generally uses dynamic allocation internally (not very `constexpr`-friendly). However, you can concatenate two or more string *literals* by just placing them in sequence. – Cheers and hth. - Alf Jan 04 '16 at 11:37
  • @Cheersandhth.-Alf yes but I've lots of long strings that I need to concatenate in front or sometimes back of strings. Kind of like escape codes, so manually placing and editing each one of them might get clumsy soon. – Abhinav Gauniyal Jan 04 '16 at 11:53
  • You could use char sequence, something like http://stackoverflow.com/a/22067775/2684539 – Jarod42 Jan 04 '16 at 12:08
  • @Jarod42 that's too much trouble, I would just throw a hacky macro approach if nothing works. – Abhinav Gauniyal Jan 04 '16 at 12:31
  • for string literals see http://stackoverflow.com/questions/13292237/c-concat-two-const-char-string-literals. – Slava Mar 22 '17 at 18:40
  • 1
    https://akrzemi1.wordpress.com/2017/06/28/compile-time-string-concatenation/ – Bart Louwers Sep 11 '17 at 14:06

1 Answers1

6

std:string::operator+() isn't a constexpr, in fact it's usually implemented in a very dynamic manner that relies on heap memory. You can append static string constants like this:

"append this string " " to this string"
Paul Evans
  • 27,315
  • 3
  • 37
  • 54