【问题标题】:What is the meaning of "C4649: attributes are ignored in this context"?“C4649:在此上下文中忽略属性”是什么意思?
【发布时间】:2017-10-23 09:30:36
【问题描述】:

这个警告是什么意思?

这里是 mcve。

template<class K> class TTT{
    public: alignas(alignof(K)) 
    union{ 
        char raw[sizeof(K)];        
        K rawK;
    }; //<-- error at this line
};

如果我在 Visual Studio 2015 中使用 ctrl+F7 编译此单个文件,我将收到此警告。

warning C4649: attributes are ignored in this context
note: see reference to class template instantiation 'TTT<K>' being compiled

我出现在我的电脑中,但http://rextester.com 无法重现此警告。

其他信息:-

  • 请注意,TTT&lt;K&gt; 从未真正实例化。
  • 如果我删除了alignas(alignof(K)) 这个词,警告就会消失。
  • 通过一些测试用例,这个类实际上是可用的。

我真的找不到任何有关于它的有用描述的网站。

有人遇到过吗?

【问题讨论】:

  • 在 VS2017 中,我得到 error C2024: 'alignas' 属性仅适用于变量、数据成员和标签类型 这可能是您的(可能是较旧的)VS 版本应该具有的说,如果对alignas/alignof有更好的支持。
  • @Rook 有答案。我建议你发布它以便它可以被接受。
  • @Rook 谢谢!是的,Rook 有答案!
  • 其实,“某个程序员老兄”已经给出了答案,所以我很高兴我没有急于发帖!快速浏览一下cppreference docs 表明它可以适用于unions,并给出了使用结构等的示例。
  • @Rook - 但是你引用的警告文本说“和标签类型”。这就是结构/类/联合。不难推测它应用不当,不知何故:)

标签: c++ visual-c++ alignment c++14 compiler-warnings


【解决方案1】:

阅读例如this alignas reference 应该放在 structunion 关键字和结构/联合标记之间。

所以应该是这样的

template<class K> struct TTT{
    union alignas(alignof(K)) {
    //    ^^^^^^^^^^^^^^^^^^^
    //    Note placement
        char raw[sizeof(K)];        
        K rawK;
    };
};

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多