簡體   English   中英

GCC 4.7.1帶有重載的廣義常量表達式問題

[英]GCC 4.7.1 generalized constant expression issue with overload

我嘗試使用模板特化實現編譯時算法選擇。

我哈希以下代碼:

template <class C>
    struct choose
    { 
        typedef size_t (*type)(const C*);
        static constexpr type value = java_string_hashcode<C>;
    };

我將此結構專門用於char類型:

template <>
    struct choose<char>
    { 
        typedef size_t (*type)(const char*);
        static constexpr type value = fnv_1a_32_hash;
    };

但是當我嘗試編譯它時,我得到GCC 4.7.1的以下錯誤:

錯誤:字段初始值設定項不是常量

我認為問題來自於fnv_1a_32_hash函數被重載的事實,即使IMO隱式轉換為size_t (*)(const char*)應該處理這個問題。

我終於找到了一個解決方法,通過重命名重載或簡單地轉換賦值:

static constexpr type value = (type)fnv_1a_32_hash;

我的問題是 :這是一個編譯器錯誤嗎? 或者我錯過了什么? 請在需要時解釋並引用規格。


fnv_1a_32_hash實施細節:

constexpr size_t fnv_1a_32_hash(const char* p, size_t h) noexcept
{ 
    return (*p == 0) ? h : fnv_1a_32_hash(p + 1, (h ^ *p) * fnv::prime);
} 

constexpr size_t fnv_1a_32_hash(const char* p) noexcept
{ 
    return fnv_1a_32_hash(p, fnv::offset_basis);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM