简体   繁体   中英

GCC 4.7.1 generalized constant expression issue with overload

I try to implement compile-time algorithm selection using template specialization.

I hash the following code:

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

I've specialized this structure for char type:

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

But when I try to compile it, I get the following error with GCC 4.7.1:

error: field initializer is not constant

I think the problem comes from the fact the fnv_1a_32_hash function is overloaded, even if IMO the implicit cast to size_t (*)(const char*) should deal with this problem.

I've finally found a workaround, by either renaming the overload OR simply casting the assignation:

static constexpr type value = (type)fnv_1a_32_hash;

My question is : is this a compiler bug? Or am I missing something? Please explain and cite specifications wherever needed.


fnv_1a_32_hash implementation details:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM