【问题标题】:How can I initialize this structure?我怎样才能初始化这个结构?
【发布时间】:2013-10-07 21:52:33
【问题描述】:

我在 clang 中遇到了编译器错误,所以我开始减少最小的测试用例。但是对于大多数简化的测试用例,GCC 告诉它包含语法错误。如何初始化这个结构?

template<typename F>
struct Item
{
  F *foo(); // with std::function clang also crash
};

template<typename F>
struct Container
{
  static const Item<F> items[];
};


template<typename F>
const Item<F> Container<F>::items[] =
{
  {  []() -> F { } }
  // {{  []() -> F { } }} this also wrong
};

template struct Container<int>;

int main()
{
  return 0;
}

GCC 说:

clangBug.cc: In instantiation of ‘const Item<int> Container<int>::items []’:
clangBug.cc:24:17:   required from here
clangBug.cc:17:15: error: too many initializers for ‘const Item<int>’

Clang master HEAD (040cd82aadd48ba50e9742881d648d53ddd2a6c9) 崩溃:

3  libc.so.6       0x00007f304c590037 gsignal + 55
4  libc.so.6       0x00007f304c593698 abort + 328
5  libc.so.6       0x00007f304c588e03
6  libc.so.6       0x00007f304c588eb2
7  clang           0x0000000000cd823e clang::Sema::InstantiateClassMembers(clang::SourceLocation, clang::CXXRecordDecl*, clang::MultiLevelTemplateArgumentList const&, clang::TemplateSpecializationKind) + 1646
8  clang           0x0000000000cd82f4 clang::Sema::InstantiateClassTemplateSpecializationMembers(clang::SourceLocation, clang::ClassTemplateSpecializationDecl*, clang::TemplateSpecializationKind) + 68
9  clang           0x0000000000c6c341 clang::Sema::ActOnExplicitInstantiation(clang::Scope*, clang::SourceLocation, clang::SourceLocation, unsigned int, clang::SourceLocation, clang::CXXScopeSpec const&, clang::OpaquePtr<clang::TemplateName>, clang::SourceLocation, clang::SourceLocation, llvm::MutableArrayRef<clang::ParsedTemplateArgument>, clang::SourceLocation, clang::AttributeList*) + 2225
10 clang           0x000000000097cb65 clang::Parser::ParseClassSpecifier(clang::tok::TokenKind, clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, bool, clang::Parser::DeclSpecContext, clang::Parser::ParsedAttributesWithRange&) + 5733
11 clang           0x0000000000962dd7 clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*) + 3511
12 clang           0x00000000009c4a98 clang::Parser::ParseSingleDeclarationAfterTemplate(unsigned int, clang::Parser::ParsedTemplateInfo const&, clang::ParsingDeclRAIIObject&, clang::SourceLocation&, clang::AccessSpecifier, clang::AttributeList*) + 632
13 clang           0x00000000009c3fda clang::Parser::ParseExplicitInstantiation(unsigned int, clang::SourceLocation, clang::SourceLocation, clang::SourceLocation&, clang::AccessSpecifier) + 154

【问题讨论】:

  • 报告错误是针对上游开发渠道的,如果没有维度 IYAM,成员 [] 也是不规范的
  • @sehe: hx,但是使用 [1] 语法会发生同样的事情

标签: c++ templates gcc c++11 clang


【解决方案1】:

如果我正确理解您的问题,您希望 Item&lt;F&gt;::foo 成为指向不带参数的函数的数据成员,并按值返回 F。您现在拥有的是一个成员函数foo 的声明,它不接受任何参数,并返回F *。将定义更改为:

template<typename F>
struct Item
{
  F (*foo)();
};

另外,正如sehe 在comments 中所说,我认为您必须为Container&lt;F&gt;::items 数组提供大小。并且初始化器中的 lambda 需要返回一个值。

template<typename F>
const Item<F> Container<F>::items[N] =
{
  {  []() -> F { return F(); } }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多