xcode 如何在编译时静态比较两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27490858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you compare two character strings statically at compile time
提问by Avner Barr
I would like to create a macro which can compare 2 strings, and emit a compile time error if the condition isn't met. This could be though of as a compile time assertion.
我想创建一个可以比较 2 个字符串的宏,并在不满足条件时发出编译时错误。这可能被认为是编译时断言。
I'm not sure how I could do this.
我不知道我怎么能做到这一点。
For instance:
例如:
STATIC_COMPARE("THIS STRING","THIS STRING") -> would emit a compile time error
STATIC_COMPARE("THIS STRING","THIS OTHER STRING) -> wouldn't emit a compile time error.
The macro would look something like
宏看起来像
#define STATIC_COMPARE(str1,str2) if (str1==str2) emit an error with a message
So I guess the question boils down to being able to compare the 2 strings at compile time.
所以我想这个问题归结为能够在编译时比较 2 个字符串。
回答by cdhowie
You can do this with C++11 by using a constexpr
function:
您可以通过使用constexpr
函数在 C++11 中执行此操作:
constexpr bool strings_equal(char const * a, char const * b) {
return *a == *b && (*a == '#include <string_view>
constexpr bool strings_equal(char const * a, char const * b) {
return std::string_view(a)==b;
}
int main() {
static_assert(strings_equal("abc", "abc" ), "strings are equal");
static_assert(!strings_equal("abc", "abcd"), "strings are not equal");
return 0;
}
' || strings_equal(a + 1, b + 1));
}
(见演示)
It's not possible to do this prior to C++11, with the caveat that many compilers will compile equal string literals to be a pointer to the same location. On these compilers it's sufficient to compare the strings directly since they will both be evaluated as equal pointers.
在 C++11 之前不可能这样做,但需要注意的是,许多编译器会将相等的字符串文字编译为指向同一位置的指针。在这些编译器上,直接比较字符串就足够了,因为它们都将被评估为相等的指针。
回答by Jan Herrmann
Starting with C++17 std::string_viewis available. It supports constexpr comparisson:
从 C++17 std::string_view 开始可用。它支持 constexpr 比较:
constexpr bool equal( char const* lhs, char const* rhs )
{
while (*lhs || *rhs)
if (*lhs++ != *rhs++)
return false;
return true;
}
回答by Columbo
You can use constexpr
functions. Here's the C++14 way:
您可以使用constexpr
函数。这是 C++14 的方式:
constexpr bool isequal(char const *one, char const *two)
{
return (*one && *two) ? (*one == *two && isequal(one + 1, two + 1)) : (!*one && !*two);
}
static_assert(isequal("foo", "foo"), "this should never fail");
static_assert(!isequal("foo", "bar"), "this should never fail");
Demo.
演示。
回答by NathanOliver
This can be done in C++ 11 using constexpr. By defining a recursive function you can check that the string are equal or not.
这可以在 C++ 11 中使用 constexpr 完成。通过定义递归函数,您可以检查字符串是否相等。
##代码##This code I used thanks to Johannes Schaub and you can see the full SO post here
由于 Johannes Schaub,我使用了这段代码,您可以在此处查看完整的 SO 帖子