Visual C++是否有__builtin_constant_p()?

cxx*_*xxl 7 c++ windows visual-studio visual-c++

是否有像__builtin_constant_p()Microsoft Visual Studio的GCC一样的功能?据我所知,如果参数是常量,函数返回非零,就像字符串文字一样.

在这里的答案(如何拥有"constexpr和运行时"别名)是一个很好的用例.

编辑: 我的想法不是写一些像:

#include <string.h>
int foo() {
   return strlen("text");
}
Run Code Online (Sandbox Code Playgroud)

我可以写:

#include <string.h>
// template_strlen() would be a function that gets the length of a compile-time     const string via templates
#define STRLEN(a) (__builtin_constant_p(a) ? template_strlen(a) : strlen(a))
int foo() {
   return STRLEN("text");
}
Run Code Online (Sandbox Code Playgroud)

(我猜这是关于在链接问题中写的内容.)我需要的只是一个变体__builtin_constant_p().

Ben*_*Key -5

在 Visual Studio 2012 和 Visual Studio 2013 中,有 _IS_LITERAL_TYPE 宏,它使用 std::is_literal_type,该宏记录在http://www.cplusplus.com/reference/type_traits/is_literal_type/中。

以下是 is_literal_type 文档的相关摘录。

"""Trait 类,用于标识 T 是否为文字类型。

文字类型是可以作为 constexpr 的类型。"""

也许这就足够了。

以下 __builtin_constant_p 文档的摘录让我相信它会的。

“您可以使用内置函数 __builtin_constant_p 来确定某个值在编译时是否已知为常量...”

对我来说,短语“是文字类型”、“constexpr”和“已知在编译时是常量”具有相同的含义。也许我错了。

话又说回来,我将是第一个承认我不确定的人。

  • 但我在编译时需要这些信息,因此它无法帮助我。 (3认同)