Templating specialization - different results with clang and gcc

I would like to specialize a template structure based on a given type like this:

#include <iostream>
#include <array>

template<typename K>
struct Foo
{
    static constexpr unsigned value = 1;
};

template<typename K, unsigned CC>
struct Foo<std::array<K,CC>>
{
    static constexpr unsigned value = CC;
};

int main()
{
    std::cout << Foo<float>::value << std::endl; // should give 1
    std::cout << Foo<std::array<char,3>>::value << std::endl; // should give 3
}

      

However, this produces different results with clang and gcc. I would say clang is doing it right and gcc is wrong.

Gcc 4.8.2-19ubuntu1 output:

1
1

      

Clang 3.4-1ubuntu3 output:

1
3

      

Questions

  • Is the observed behavior a bug with gcc?
  • What would be the correct approach / workaround that also works with gcc?
+3


source to share


2 answers


std :: array parameter to the second template size_t

, not unsigned.

So if you rewrite your function like



template<typename K, std::size_t CC>
struct Foo<std::array<K,CC>>
{
    static constexpr unsigned value = CC;
};

      

both compilers should give equal results.

+2


source


This is a known bug in clang: g ++ and clang ++ different behavior with integral template parameter , http://llvm.org/bugs/show_bug.cgi?id=16279

17 - [...] [Template type] must match exactly the type of template-parameter, except that the template argument inferred from the array binding can be of any integer type.



The correct approach is to use std::size_t

the template parameter as an integer type Foo

.

0


source







All Articles