通过lambda初始化constexpr变量

时间:2016-08-09 05:10:53

标签: c++ c++11 gcc clang constexpr

public IEnumerable<AcademicLevel> GetAcademicLevels()
{
   //  blah  blah blah ...

   var academicLevel = academicRepository.SelectAll().ToList();

   return academicLevel;
}

命令

#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;
}

给出

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

g ++版本:

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

在gcc版本5.2.1(linux)上编译没有任何问题。

0 个答案:

没有答案