vendredi 3 juillet 2015

Unexpected result for a type counter using templates with function local types in Clang

I wrote a class template based on two types that is assigned a unique index based on its template parameters:

template<typename SK,typename T>
struct Component {
    static uint const index;
};

The expectation is that for each new type, index is incremented:

Component<X,A>::index; // 0
Component<X,B>::index; // 1

Component<Y,A>::index; // 0
Component<Y,B>::index; // 1
// ...etc

The complete code that assigns the indices is as follows:

using uint = unsigned int;

template<typename SK,typename T>
struct Component
{
    static uint const index;
};

template<typename SK>
class ComponentCount
{
    template<typename CSK,typename CT>
    friend struct Component;

private:
    template<typename T>
    static uint next() {
        return ComponentCount<SK>::get_counter();
    }

    static uint get_counter()
    {
        static uint counter = 0;
        return counter++;
    }
};

This works as expected in GCC (5.1) and MSVC with the following test:

// global scope
struct X {};
struct Y {};

int main()
{
    // function scope
    struct Z{};

    uint x0 = Component<X,int>::index;
    uint x1 = Component<X,double>::index;
    uint x2 = Component<X,double>::index;
    uint x3 = Component<X,std::string>::index;
    uint x4 = Component<X,int>::index;
    uint x5 = Component<X,int>::index;

    std::cout << x0 << ", " << x1 << ", " << x2 << ", "
              << x3 << ", " << x4 << ", " << x5 << std::endl;

    uint y0 = Component<Y,int>::index;
    uint y1 = Component<Y,double>::index;
    uint y2 = Component<Y,double>::index;
    uint y3 = Component<Y,std::string>::index;
    uint y4 = Component<Y,int>::index;
    uint y5 = Component<Y,int>::index;

    std::cout << y0 << ", " << y1 << ", " << y2 << ", "
              << y3 << ", " << y4 << ", " << y5 << std::endl;

    uint z0 = Component<Z,int>::index;
    uint z1 = Component<Z,double>::index;
    uint z2 = Component<Z,double>::index;
    uint z3 = Component<Z,std::string>::index;
    uint z4 = Component<Z,int>::index;
    uint z5 = Component<Z,int>::index;

    std::cout << z0 << ", " << z1 << ", " << z2 << ", "
              << z3 << ", " << z4 << ", " << z5 << std::endl;

    return 0;
}

The output is

0, 1, 1, 2, 0, 0
0, 1, 1, 2, 0, 0
0, 1, 1, 2, 0, 0

However with Clang (3.6.1), the output differs:

0, 1, 1, 2, 0, 0
0, 1, 1, 2, 0, 0
5, 2, 2, 3, 5, 5

Specifically, the indices generated for function local types (ie. 'Z') do something strange. Its like they increment and reassign the index every time Component<Z,...> is called.

Why does this happen? Is it a compiler bug? Are there any special considerations when using function local types with templates (post C++11)?

A complete example can be found here: http://ift.tt/1HCzbsy

Aucun commentaire:

Enregistrer un commentaire