Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ gcc上静态成员变量编译错误的概念检查_C++_Templates_Sfinae - Fatal编程技术网

C++ gcc上静态成员变量编译错误的概念检查

C++ gcc上静态成员变量编译错误的概念检查,c++,templates,sfinae,C++,Templates,Sfinae,我正在尝试应用中描述的技术 使用下面的示例代码,我希望得到以下输出: 1 0 0 0 01 0 0 01 0 01 如果我使用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

我正在尝试应用中描述的技术

使用下面的示例代码,我希望得到以下输出:

1 0 0 0
01 0 0
01 0
01

如果我使用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'
junk.cpp:const bool has\u foo\u member\u variable::value'的实例化中:
junk.cpp:45:5:从“void print()[with T=B]实例化”
junk.cpp:82:14:从此处实例化
junk.cpp:30:75:错误:“B::foo”不是类型“int B::*”的有效模板参数
junk.cpp:30:75:错误:它必须是指向格式为“&X::Y”的成员的指针
junk.cpp:在“const bool has_foo_member_variable::value”的实例化中:
junk.cpp:45:5:从“void print()[with T=D]实例化”
junk.cpp:84:14:从此处实例化
junk.cpp:30:75:错误:“&D::foo”不是类型“int D::*”的有效模板参数
junk.cpp:30:75:错误:它必须是指向格式为“&X::Y”的成员的指针
我正在使用GCC4.5.1。。。我看起来gcc没有遵循正确的SFINAE规则,但我不是100%确定。clang正确吗?这是一个gcc错误

#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>();
}
#包括
struct small_type{char dummy;};
结构大型_类型{char dummy[2];};
模板
结构具有\u foo\u成员\u函数
{
模板结构测试仪;
模板静态小型_类型有_foo(测试仪*);
模板静态大_类型有_foo(…);
静态常量布尔值=(sizeof(has_foo(0))==sizeof(small_type));
};
模板
结构具有\u foo\u静态\u成员\u函数
{
模板结构测试仪;
模板静态小型_类型有_foo(测试仪*);
模板静态大_类型有_foo(…);
静态常量布尔值=(sizeof(has_foo(0))==sizeof(small_type));
};
模板
结构具有\u foo\u成员\u变量
{
模板结构测试仪;
模板静态小型_类型有_foo(测试仪*);
模板静态大_类型有_foo(…);
静态常量布尔值=(sizeof(has_foo(0))==sizeof(small_type));
};
模板
结构具有\u foo\u静态\u成员\u变量
{
模板结构测试仪;
模板静态小型_类型有_foo(测试仪*);
模板静态大_类型有_foo(…);
静态常量布尔值=(sizeof(has_foo(0))==sizeof(small_type));
};
模板
作废打印()
{

std::cout您的代码是正确的

clang正确吗?这是一个gcc错误

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

<>是的,很可能是。确认你的代码是正确的。< /P>我不能确认这是否正确C++。但是如果多数的计数对任何东西都有好处,我可以确认一个编译器。VisualC++没有问题,输出完全是你所描述的。