簡體   English   中英

靜態成員變量的概念檢查在gcc上編譯錯誤

[英]Concept checking of static member variables compile error on gcc

我正在嘗試應用http://www.drdobbs.com/tools/227500449中描述的技術

使用下面的示例代碼,我期望輸出:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

如果我使用clang進行編譯,這確實會發生什么。 但是使用gcc,此代碼會出現以下錯誤:

junk.cpp: In instantiation of ‘const bool has_foo_member_variable<B>::value’:
junk.cpp:45:5:   instantiated from ‘void print() [with T = B]’
junk.cpp:82:14:   instantiated from here
junk.cpp:30:75: error: ‘B::foo’ is not a valid template argument for type ‘int B::*’
junk.cpp:30:75: error: it must be a pointer-to-member of the form `&X::Y'
junk.cpp: In instantiation of ‘const bool has_foo_member_variable<D>::value’:
junk.cpp:45:5:   instantiated from ‘void print() [with T = D]’
junk.cpp:84:14:   instantiated from here
junk.cpp:30:75: error: ‘& D::foo’ is not a valid template argument for type ‘int D::*’
junk.cpp:30:75: error: it must be a pointer-to-member of the form `&X::Y'

我正在使用gcc 4.5.1 ...我看起來像gcc沒有遵循正確的SFINAE規則,但我不是100%肯定。 clang是否正確,這是一個gcc bug?

#include <iostream>

struct small_type { char dummy; };
struct large_type { char dummy[2]; };

template<class T>
struct has_foo_member_function
{
    template<int (T::*)()> struct tester;
    template<class U> static small_type has_foo(tester<&U::foo> *);
    template<class U> static large_type has_foo(...);
    static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};

template<class T>
struct has_foo_static_member_function
{
    template<int (*)()> struct tester;
    template<class U> static small_type has_foo(tester<&U::foo> *);
    template<class U> static large_type has_foo(...);
    static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};

template<class T>
struct has_foo_member_variable
{
    template<int T::*> struct tester;
    template<class U> static small_type has_foo(tester<&U::foo> *);
    template<class U> static large_type has_foo(...);
    static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};

template<class T>
struct has_foo_static_member_variable
{
    template<int *> struct tester;
    template<class U> static small_type has_foo(tester<&U::foo> *);
    template<class U> static large_type has_foo(...);
    static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};

template<class T>
void print()
{
    std::cout << has_foo_member_function<T>::value << " "
        << has_foo_static_member_function<T>::value << " "
        << has_foo_member_variable<T>::value << " "
        << has_foo_static_member_variable<T>::value << "\n";
}

struct A
{
    int foo()
    {
        return 0;
    }
};

struct B
{
    static int foo()
    {
        return 0;
    }
};

struct C
{
    int foo;
};

struct D
{
    static int foo;
};

int main()
{
    print<A>();
    print<B>();
    print<C>();
    print<D>();
}

你的代碼是正確的

clang是否正確,這是一個gcc bug?

是的,最有可能。 Comeau確認您的代碼是正確的。

暫無
暫無

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

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