Qt vs constexpr字符串文字

时间:2019-05-18 18:54:32

标签: c++ qt constexpr string-literals

是否可以在Qt中将其定义为静态constexpr字符串文字成员?即类似于以下内容:

class X
{
   static constexpr QString tag = "mytag";
};

3 个答案:

答案 0 :(得分:1)

考虑到QString是一个(可能)堆分配的字符串,您不能用constexpr代码来分配内存,否。在编译时,它比使用std::string更为有效。

答案 1 :(得分:1)

我做了Jesper在他的评论中建议的操作,我使用了QLatin1String。但是我通过一个小的帮助器类使用它来避免QLatin1String中的strlen()调用:

struct ConstLatin1String : public QLatin1String
{
    constexpr ConstLatin1String(const char* const s) : 
        QLatin1String(s, static_cast<int>(std::char_traits<char>::length(s))) {}
};

这可以做到:

static constexpr ConstLatin1String mystring = "foo";

答案 2 :(得分:1)

Silicomancer answer启发,但可与c++11一起使用

#include <QLatin1String>

namespace detail {

// Helper function for getting compile-time string literal's length 
// Given from: https://stackoverflow.com/q/27498592/ 
// c++11 analogue of: std::char_traits<char>::length(str); from c++14
constexpr std::size_t constLength(const char* str)
{
    return ((*str) == '\0') ? 0 : constLength(str + 1) + 1;
}

} // namespace detail

// Constructs compile-time <QLatin1String> from string literal
constexpr QLatin1String make_string(const char* str)
{
    return QLatin1String(str, detail::constLength(str));
}

// Alternate implementation for string literals only, without 
// recursive function call (reduces compilation time), using 
// known in compile-time size.
template <std::size_t SIZE>
constexpr QLatin1String make_string_from_literal(const char (&str)[SIZE])
{
    return QLatin1String(str, SIZE);
}

// Example of usage:
static constexpr QLatin1String STR = make_string("hello_world");

测试

// Helper function for compile-time strings comparison
// Given from https://stackoverflow.com/a/27490954/
constexpr bool strings_equal(const char* a, const char* b) {
    return (*a == *b) && (*a == '\0' || strings_equal(a + 1, b + 1));
}

// Compile-time in-place construction check
static_assert( strings_equal(make_string("foo").data(), "foo"), "test failed");

// Compile-time constant construction check
static constexpr QLatin1String STR = make_string("bar");
static_assert( strings_equal(STR.data(), "bar"), "test failed");

增加灵感

它与in that brilliant answer之类的compile-time hashing完美结合,它基于(thatthat

// Note: `len` must be without null-character sign
constexpr uint32_t crc32(const char* str, std::size_t len) {
    return detail::crc32(len - 1, str) ^ 0xFFFFFFFF;
}

template <std::size_t LEN>
constexpr std::uint32_t crc32(const char (&str)[LEN]) {
    return crc32(str, LEN - 1);
}

// Overload for <QLatin1String>
//   Notice that, next methods: QLatin1String::{.data(), .size()} 
//   marked as constexpr only since Qt 5.6. 
constexpr std::uint32_t crc32(const QLatin1String& str) {
    return crc32(str.data(), str.size());
}

// ------------------------------------------------------------

// Test with explicitely specified literal size
static_assert( crc32("hello world", 11) == 0xd4a1185,  "CRC32 test failed");
static_assert( crc32("foo bar",      7) == 0xbe460134, "CRC32 test failed");

// Test with implicitly calculated literal size
static_assert( crc32("hello world") == 0xd4a1185,  "CRC32 test failed");
static_assert( crc32("foo bar")     == 0xbe460134, "CRC32 test failed");

// Test with convenient overload for <QLatin1String>
static constexpr QLatin1String STR_1 = make_string("hello world");
static constexpr QLatin1String STR_2 = make_string("foo bar");
static_assert( crc32(STR_1) == 0xd4a1185,  "CRC32 test failed");
static_assert( crc32(STR_2) == 0xbe460134, "CRC32 test failed");

允许通过switch-case进行编译时字符串匹配。 switch的两种情况都是相等的,并且是在编译时计算的:)

    1. 在编译时构造的字符串,允许在任何情况下使用它,并在需要的地方适当地计算哈希。
    1. 从字符串中预先计算出的哈希值。字符串不存在。
    1. 无需任何仪式就地使用:D
static constexpr QLatin1String MATCH_1 = make_string("value_1"); // 1)
static constexpr auto MATCH_2 = crc32( make_string("value_2") ); // 2)

const QLatin1String str = /* ... */;
switch( crc32(str) )
{
    case crc32(MATCH_1):   { do_something_for_value_1(); } break; // 1)
    case MATCH_2:          { do_something_for_value_2(); } break; // 2)
    case crc32("value_3"): { do_something_for_value_3(); } break; // 3)
}