4
#include <iostream>
#include <string>

constexpr auto fx = [] (std::string msg) {
  return msg + "!\n"; };

int main(int argc, char* argv[]) {
  if (argc == 2)
    std::cout << "hello " << fx(argv[1]);
  else
    std::cout << "hello world!" << std::endl;
}

Command

$ g++ -o hello -std=c++11 hello.cpp

gives

hello.cpp:4:21: error: constexpr variable 'fx' must be initialized by a constant expression
constexpr auto fx = [] (std::string msg) {
                    ^~~~~~~~~~~~~~~~~~~~~~
1 error generated.

g++ version:

$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

On gcc version 5.2.1 (linux) compiles without any issues.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185

0 Answers0