代码之家  ›  专栏  ›  技术社区  ›  9301293

msvs/msvc静态模板类方法不发出警告

  •  0
  • 9301293  · 技术社区  · 6 年前

    不久前,这个 question 被问到,关于熟悉的

    error: 'static' can only be specified inside the class definition
    

    错误。

    在我当前的用例中,我将从一个非常MSVC的项目开始,在这个项目中,几乎所有的代码都是使用MSVC编译的,并且是针对Android的交叉编译。

    我注意到没有MSVC错误,尤其是警告,关于静态类方法在类内部(外部)有定义。我错过什么了吗?为什么至少没有警告?


    编辑

    为了澄清这一点,我在问为什么没有适当的MSVC/MSV警告代码(摘自上面的链接):

    class Foobar {
    public:
      static void do_something();
    };
    static void Foobar::do_something() {} // Error!
    
    int main() {
        Foobar::do_something();
    }
    

    编辑

    真抱歉,伊芙妮!这个样品不行!我很抱歉。

    class Foobar {
    public:
      template<class Y> 
      static int do_something();
    };
    
    template<class Y> 
    static int Foobar::do_something() {return 1;} // Error!
    
    int main() {
        return Foobar::do_something<double>();
    }
    

    这是输出 MSVC 19.14 (成功) GCC 4.12 (失败)。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Chuck Walbourn    6 年前

    vs 2012 update 5、vs 2013 update 5、vs 2015 update 3和vs 2017(15.9 update)都报告了此代码的错误:

    error C2724: 'Foobar::do_something': 'static' should not be used
                 on member functions defined at file scope
    

    我猜想只有用一个旧的,不符合版本的VisualC++来构建代码。

    注意,如果您想使用VisualC++编译器清理代码,以便更容易地移植到其他平台:

    使用 允许的 已经暗示 /Zc:strictStrings , /Zc:rvalueCast /Zc:ternary 使能 two-phase name look-up .

    • 您也可以使用 /Wall 尽管要降低所有噪音才能看到有用的警告,还是需要一些努力。它仍然不像 clang 但这是有帮助的。有关要抑制的一组内容的示例,请参见 this header .

    • 还有一个实验性的C99预处理器,您可以尝试,尽管它还处于早期阶段。见 this blog post

    你可以自己试试VS 2017社区版。