本文介绍了编译器在constexpr中使用strcmp的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在GCC中编译,但不是在Clang中编译: #include< cstring>

constexpr int test = strcmp(test,test);

所以我的问题是,GCC如何处理strcmp以使其成为可能? strcmp是一些内置的类型,还是它的标准库有一个非标准的strcmp定义,包括constexpr?

代码在gcc上编译,因为它提供了一个 <$假设你将字符串文字传递给函数,在编译时计算c $ c> strcmp 。

gcc会拒绝密码如果你通过 -fno-builtin (或 -fno-builtin-strcmp )标志。


The following compiles in GCC but not in Clang:

#include <cstring>

constexpr int test = strcmp("test", "test");

So my question is, how does GCC handle strcmp differently to make this possible? Is strcmp some type of builtin, or does its standard library have a non-standard definition of strcmp that includes constexpr?

解决方案

The code compiles on gcc because it provides a built-in version of strcmp that is evaluated at compile time, assuming you pass string literals to the function.

gcc will reject the code if you pass the -fno-builtin (or -fno-builtin-strcmp) flag.

这篇关于编译器在constexpr中使用strcmp的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 03:32