-3

Is the following code is correct if I have it in a header file?

template <T> Stopwatch *Stopwatch::m_instance = nullptr;
class Stopwatch
{
   std::clock_t m_lastStep;
   std::clock_t m_start;
   static Stopwatch *m_instance; 

};

ok With the help of everybody, I could manage to compile it as follow:

The idea is to have the class definition and static member initialization on the one header file. Please note that I use template but never used its type.

I remove other part of code for simplicity.

template <typename T>
class Stopwatch
{
   std::clock_t m_lastStep;
   std::clock_t m_start;
   static Stopwatch *m_instance; 

};

template <typename T>
Stopwatch<T> *Stopwatch<T>::m_instance = nullptr;

Now I need to see if it really works!

mans
  • 17,104
  • 45
  • 172
  • 321
  • 5
    No. Why don't you try compiling it? – juanchopanza Feb 11 '15 at 15:42
  • 1
    `I remove other part of code for simplicity` So how are we to know if the parts of the code you removed are required? – PaulMcKenzie Feb 11 '15 at 15:43
  • @juanchopanza VS intelisense doesn't give me any error, so I think it has no compilation error. – mans Feb 11 '15 at 15:45
  • 1
    @mans What exactly are we supposed to do? – dmg Feb 11 '15 at 15:46
  • 1
    Oh that's alright then. – juanchopanza Feb 11 '15 at 15:46
  • 1
    @mans Don't trust intellisense! Compile it. – Biffen Feb 11 '15 at 15:47
  • @dmg Creating a singleton that implemented in a header file (no CPP).Getting idea from here: http://stackoverflow.com/questions/23690416/c-template-singleton-static-pointer-initialization-in-header-file – mans Feb 11 '15 at 15:47
  • @Biffen Ok, I need to add other part of code and then compile it. – mans Feb 11 '15 at 15:48
  • 2
    There comes a time in every VS C++ developer's life when they turn off intellisense because it just doesn't work! –  Feb 11 '15 at 15:50
  • 1
    @mans Having the singleton definition (i.e. the `m_instance = nullptr` line) in the header will fail as soon as you have more than one compilation unit including this header. EDIT: Sorry, sure, by using templates you circumvent this issue. – filmor Feb 11 '15 at 15:51
  • 1
    @mans Does `m_instance` have to be a pointer? And since you set it to `nullptr`, who will initialise it? – Biffen Feb 11 '15 at 15:53
  • 1
    I believe [singletons should be avoided](http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/) anyway. – Fred Larson Feb 11 '15 at 15:53
  • I do not turn off intellisense. I just know that it will have a certain percentage of false positives. Although I do not understand why (by default) Microsoft shows intellisense errors in the same list of real compiler errors. This just confuses people who do not have experience. – drescherjm Feb 11 '15 at 18:30

2 Answers2

2

Why don't you compile it to see? Btw that code will not compile.

  • You would have to move the first line after the class definition.

  • Also, you're defining m_instance as two different kinds of symbols. You must either remove the redundant template specifier, or declare the m_instance member variable as a template variable in the class definition as well.

  • If you decided to keep it as a template variable, your template parameter T is still missing a type. Add typename or class before it to fix this.

Then it will compile.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

With the fix that the class definition should go first, it's correct, as long as you include this header file only in one *.cpp file.

Implementing static field in the header file (meaning: you'll potentially be including it in multiple *.cpp files): NOT POSSIBLE. You can't even declare it as static.

I guess the reason is that in C++ you can potentially initialize the variable by a result of a function call, so this would carry a risk that an object is doubly-initialized and lead to undefined behavior (because theoretically it could undergo vague linkage).

Ethouris
  • 1,791
  • 13
  • 18
  • That is the reason that I used template, as templates can be defined in a header file. – mans Feb 11 '15 at 15:55
  • 1
    There's no such rule as 'templates can be defined in a header file' - in a header file only such things may be defined, which **are not "physical"** (class, integer constant), or - if they are - they either **do not undergo linkage** (static functions or variables) or **undergo vague linkage** (inline functions, which may result in out-of-line versions, or various C++-generated objects like virtual tables or RTTI objects). The special thing about templates is that they **must** be defined in the header file (no *export*), that is, it must be a class or inline function (nothing else can be tpl) – Ethouris Feb 11 '15 at 16:02