Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 889
Geoffroy Avatar asked Nov 14 '22 03:11

Geoffroy


1 Answers

Probably already reported:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52366

like image 58
PiotrNycz Avatar answered Dec 19 '22 02:12

PiotrNycz