c ++ 11:字符串文字的恒定时间查找功能

时间:2014-03-29 16:18:12

标签: c++ templates c++11 template-meta-programming

C++中,可以在编译时以下列方式生成对变量值的整数的常量查找:

template<int>
int *map() {
    static int var = 0;
    return &var;
}

int main() {
    *map<0>() = 42;
    *map<1>() = 1337;

    return *map<0>(); //returns 42
}

请注意,编译器将为编译时使用的每个“键”创建一个全局变量map<key>::var

是否可以创建一个使用字符文字作为“键”的类似地图功能?请注意,由于字符链的本地链接,字符文字可以用作模板参数。

我需要能够在代码的任何部分指定新密钥,事实上,作为任何表达式的一部分。请注意,在我的整数示例中,我指定map<0>只应存在于main()

注意:特别是,我想使用__FILE__, __LINE__的元组作为关键字,通过在var前添加thread_local和特定于翻译单元,使地图特定于线程。使用map()static添加前缀。因此,从理论上讲,字符文字的局部联系不会造成问题。整个过程是对记录器的性能优化,它允许为特定文件的各个部分指定日志级别。

2 个答案:

答案 0 :(得分:2)

这里有一个概念证明,即使我不推荐它implementation,请注意这个使用c ++ 1y以方便constexpr:

#include <iostream>

constexpr bool cstrcmp( char const * s1, char const * s2 ) {
    while ( *s2 && *s1 ) {
        if (*s1++ != *s2++ )
            return false;
    }
    return !*s1 && !*s2;
}

constexpr int str_to_val( char const * str ) {
    struct pair { char const*str; int value; };
    constexpr pair const tab[] { {"foo",1}, {"bar", 2} };
    for( auto & e : tab ) {
        if ( cstrcmp(str,e.str) )
            return e.value;
    }
    throw 0;
}

int main() {
    constexpr auto test_0 = str_to_val("foo");
    constexpr auto test_1 = str_to_val("bar");
    //constexpr auto test_2 = str_to_val("baz"); // trigger compilation error
    std::cout << test_0 << " " << test_1 << std::endl;
}

答案 1 :(得分:1)

您可以将文字字符串转换为自定义类型 以下内容可能有所帮助:(https://ideone.com/DPQQyD

#include <cstdint>

// Sequence of char
template <char... Cs> struct char_sequence
{
    template <char C> using push_back = char_sequence<Cs..., C>;
};

// Remove all chars from char_sequence from '\0'
template <typename, char...> struct strip_sequence;

template <char... Cs>
struct strip_sequence<char_sequence<>, Cs...>
{
    using type = char_sequence<Cs...>;
};

template <char...Cs, char...Cs2>
struct strip_sequence<char_sequence<'\0', Cs...>, Cs2...>
{
    using type = char_sequence<Cs2...>;
};

template <char... Cs, char C, char... Cs2>
struct strip_sequence<char_sequence<C, Cs...>, Cs2...>
{
    using type = typename strip_sequence<char_sequence<Cs...>, Cs2..., C>::type;
};

// helper to get the i_th character (`\0` for out of bound)
template <std::size_t I, std::size_t N>
constexpr char at(const char (&a)[N]) { return I < N ? a[I] : '\0'; }

// helper to check if the c-string will not be truncated
template <std::size_t max_size, std::size_t N>
constexpr bool check_size(const char (&)[N])
{
    static_assert(N <= max_size, "string too long");
    return N <= max_size;
}

// Helper macros to build char_sequence from c-string
#define PUSH_BACK_8(S, I) \
    ::push_back<at<(I) + 0>(S)>::push_back<at<(I) + 1>(S)> \
    ::push_back<at<(I) + 2>(S)>::push_back<at<(I) + 3>(S)> \
    ::push_back<at<(I) + 4>(S)>::push_back<at<(I) + 5>(S)> \
    ::push_back<at<(I) + 6>(S)>::push_back<at<(I) + 7>(S)>

#define PUSH_BACK_32(S, I) \
        PUSH_BACK_8(S, (I) + 0) PUSH_BACK_8(S, (I) + 8) \
        PUSH_BACK_8(S, (I) + 16) PUSH_BACK_8(S, (I) + 24)

#define PUSH_BACK_128(S, I) \
    PUSH_BACK_32(S, (I) + 0) PUSH_BACK_32(S, (I) + 32) \
    PUSH_BACK_32(S, (I) + 64) PUSH_BACK_32(S, (I) + 96)

// Macro to create char_sequence from c-string (limited to 128 chars)
#define MAKE_CHAR_SEQUENCE(S) \
    strip_sequence<char_sequence<> \
    PUSH_BACK_128(S, 0) \
    >::type::template push_back<check_size<128>(S) ? '\0' : '\0'>