【发布时间】:2018-08-01 14:22:37
【问题描述】:
似乎编译器在将 constexpr 限定符添加到非 constexpr 函数时会忽略它。这是为什么?
以下代码可以正常编译并运行。
#include <iostream>
#include <string>
using std::string; using std::cout; using std::endl;
constexpr bool is_shorter(const string &lft, const string &rht) // this is not a constexpr function
{
return lft.size() < rht.size();
}
int main()
{
bool restul = is_shorter("Hello", "World!");
return 0;
}
【问题讨论】:
-
它只适用于某些编译器。最好检查一下标准对此的规定。
-
@AndreySemenov 我不知道为什么,我在我的机器上成功编译。我正在使用 cmake 完成所有工作并将其设置为 c++ 11。这可能是个问题?
标签: c++ function c++11 constexpr