0

A while ago, this question was asked, regarding the familiar

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

error.

In my current use-case, I am moving from a very MSVC project, where almost all the code is compiled using MSVC, and cross compiling for Android.

I noticed that there is no MSVC error, least of all, a warning, about static class methods having definitions inside (outside) the class. Am I missing something? Why is there not at least a warning?


EDIT

To clarify, I am asking why there is no proper MSVC/MSVS warning for code such as this (taken from the link, above):

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

int main() {
    Foobar::do_something();
}

EDIT

So sorry evey one! This sample doesn't work! My apologies.

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

Here is the output from MSVC 19.14 (success), and GCC 4.12 (failure).

9301293
  • 514
  • 3
  • 16
  • We need an [MCVE] in which you'd expect a warning. – tkausl Nov 16 '18 at 17:55
  • It's perfectly OK (and normal) to have static function definitions outside the class - but you can't label them as `static` in that context. –  Nov 16 '18 at 17:56
  • Remove the `static` keyword from the out of line defenitions? I'm unclear to what you are trying to ask here – NathanOliver Nov 16 '18 at 17:56
  • @tkausl One such example is provided in the hyperlink in the post. – 9301293 Nov 16 '18 at 17:57
  • @NeilButterworth see edit. I understand that the solution is to delete the `static` word, prior to the definition. That is why I have included the previous post. – 9301293 Nov 16 '18 at 18:00
  • 2
    [Can't reproduce](https://godbolt.org/z/HFysEM) – tkausl Nov 16 '18 at 18:05
  • Note that you will get the error message regardless of whether `do_something` is a template or not. Unless you are using some strange old version of MSVC. – Christian Hackl Nov 16 '18 at 18:23

1 Answers1

2

VS 2012 Update 5, VS 2013 Update 5, VS 2015 Update 3 and VS 2017 (15.9 update) all report an error for this code:

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

I'm guessing the code built only with an older, non-conformant version of Visual C++.

Note that if you want to clean up code using the Visual C++ compiler to make it easier to port to other platforms:

  • You can use VS 2017 with the /permissive- switch. See this blog post.

  • There are a number of conformance switches to try as well such as /Zc:__cplusplus. See this blog post.

Using /permissive- already implies /Zc:strictStrings, /Zc:rvalueCast, and /Zc:ternary and enables two-phase name look-up.

  • You can also use /Wall, although it takes some effort to get all the noise down to see the useful warnings. It's still not as fussy as clang, but it's helpful. For an example of a bunch of stuff to suppress, see the top of this header.

  • There is also an experimental C99 preprocessor you can try, although that one is still in the early stages. See this blog post

You can try it out yourself with the VS 2017 Community edition.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81
  • @ChristianHackl Big error on my part. Please, see the edit. – 9301293 Nov 16 '18 at 18:15
  • 1
    Thats why I asked half an hour ago a snipped/MCVE of __your__ code, not just _any_ code. – tkausl Nov 16 '18 at 18:19
  • 1
    `/Wall` is completely useless because it is incompatible with standard C++ headers. Compiling with `/W4`, however, is a good idea. – Christian Hackl Nov 16 '18 at 18:25
  • @ChristianHackl - ``Wall`` turns on a bunch of off-by-default stuff that is useful if you take the time to silence all the stuff that isn't... I use it for my various GitHub projects, although I agree it's a lot of work. See also [this blog post](https://blogs.msdn.microsoft.com/vcblog/2017/12/13/broken-warnings-theory/) – Chuck Walbourn Nov 16 '18 at 18:28
  • @ChuckWalbourn: I don't know... I get a few pages of warnings if I just include ``, without even *using* anything from the header. Perhaps what works best is compiling with `/W4` in your project's standard configuration and inspecting the `/Wall` results only every once in a while to avoid wasting time and resources. – Christian Hackl Nov 16 '18 at 18:30
  • There's also the opposite approach of turning on [off-by-default warnings](https://learn.microsoft.com/en-us/cpp/preprocessor/compiler-warnings-that-are-off-by-default) that are most useful for you. In any case, Visual C++ does have a lot more warnings it can emit even if it doesn't in most cases. I'm wondering what version if MSVC the OP's code actually built with... VS 2010? VS .NET 2008? I don't have those toolsets on my computer anymore and honestly I should trim VS 2012/VS 2013 :) – Chuck Walbourn Nov 16 '18 at 18:35