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 4.7有时无法使用指针模板参数_C++_Templates_Gcc_C++11_Compiler Bug - Fatal编程技术网

C++ gcc 4.7有时无法使用指针模板参数

C++ gcc 4.7有时无法使用指针模板参数,c++,templates,gcc,c++11,compiler-bug,C++,Templates,Gcc,C++11,Compiler Bug,完整(非)工作示例: struct s { int i; }; template<const s* _arr> class struct_array { public: static constexpr auto arr = _arr[0]; // works template<int > struct inner // line 9, without this struct, it works { }; }; cons

完整(非)工作示例:

struct s { int i; };

template<const s* _arr>
class struct_array
{
public:
    static constexpr auto arr = _arr[0]; // works
    template<int >
    struct inner // line 9, without this struct, it works
    {    
    };
};

constexpr const s s_objs[] = {{ 42 }};

int main()
{
    struct_array<s_objs> t_obj;
    return 0;
}
我使用ideone的gcc 4.8.1运行了一个程序,但4.7.3向我打印了以下内容:

constexpr.cpp: In instantiation of ‘class struct_array<((const s*)(& s_objs))>’:
constexpr.cpp:18:30:   required from here
constexpr.cpp:9:16: error: lvalue required as unary ‘&’ operand
constexpr.cpp:9:16: error: could not convert template argument ‘(const s*)(& s_objs)’ to ‘const s*’
constepr.cpp:在“类结构数组”的实例化中:
constexpr.cpp:18:30:从这里开始需要
constexpr.cpp:9:16:错误:一元'&'操作数需要左值
constexpr.cpp:9:16:错误:无法将模板参数“(const s*)(&s_objs)”转换为“const s*”

最后两行重复3次。原因是什么,在GCC4.7.3上使用我的代码有什么解决方法吗?

这对我来说似乎是一个编译器错误

我在gcc 4.1.2()上尝试了您的示例,您必须明确地注意到该变量具有外部链接(const暗示内部链接,除非另有规定,否则以下代码为C++03):

如果希望变量是静态类成员,则必须将其指定为具有外部链接:

struct data
{
    static const s s_objs[1];
};

extern const s data::s_objs[1] = {{ 42 }};

这给了我一个答案,不是4.8。还有它。因此,我不确定这是纯标准的还是某个编译器中的错误。

我强烈建议您尝试使用新的编译器,避免为编译器错误采取愚蠢的解决方法。虽然这在所有情况下都不太可行。哇,谢谢!如果
s_objs
应该是静态类成员(某个类的成员)而不是全局变量,那么声明会是什么?这还可能吗?我的编译器说,
extern
static
是冲突的。。。
struct s { int i; };

template<const s* _arr>
class struct_array
{
public:
    static const s arr;
    template<int >
    struct inner
    {    
    };
};

template<const s* _arr>
const s struct_array<_arr>::arr = _arr[0];

// Notice the 'extern'
extern const s s_objs[] = {{ 42 }};

int main()
{
    struct_array<s_objs> t_obj;
    return 0;
}
constexpr const s s_objs[] = ...;
extern const s s_objs[] = ...;
struct data
{
    static const s s_objs[1];
};

extern const s data::s_objs[1] = {{ 42 }};