0


First of all: I am new to C++, so don't judge me. :)
I tried defining a static weak_ptr in a template class in order to use it over all instances.
This is my code:

template <class T> class my_template : public my_class {

    protected:
        std::shared_ptr<T> sp;

        virtual bool init_impl() {
            sp = wp.lock();
            ...
            return true;
        }

    private:
        static std::weak_ptr<T> wp;
};

But compiling gives me the error:

.../my_template.hpp:7: undefined reference to 'my_template<my_class2>::wp'

Can anyone help? I just don't get it.

p0wl90
  • 87
  • 1
  • 6
  • You're only *declaring* the static member, you have to actually define it as well. There are many duplicates here on SO on the very subject. – Some programmer dude Jun 14 '15 at 15:55
  • Sorry, but I could not find one, where the variable is used directly above the definition. They all define it in a .cpp or something, which does not help me. – p0wl90 Jun 14 '15 at 15:57

2 Answers2

1

Just add in the same header:

template<class T>
std::weak_ptr<T> my_template<T>::wp;

Works for me! :)

peper0
  • 3,111
  • 23
  • 35
  • Every source file that uses this header file will get its own copy of the `wp` variable, they will not be shared. – Remy Lebeau Jun 14 '15 at 18:11
  • @RemyLebeau Not true: see this answer: http://stackoverflow.com/questions/3229883/static-member-initialization-in-a-class-template – Daniele Pallastrelli Jun 14 '15 at 18:18
  • @DanielePallastrelli: see this answer: http://stackoverflow.com/a/7109126/65863 - "If you plan on using explicit instantiations in your code, ... then you must not put the templated definition in the header if [the template] is also used in other TUs other than the one containing the explicit instantiation, since **you'd then get multiple definitions**." – Remy Lebeau Jun 14 '15 at 18:22
  • 1
    @RemyLebeau: the answer you linked describe another case. Still, it's not true that yout will get one copy per compilation unit. – peper0 Jun 14 '15 at 19:27
  • 1
    @RemyLebeau The code in the answer *is not* an explicit instantiation, so you don't get multiple definitions. – Daniele Pallastrelli Jun 14 '15 at 20:22
  • Thank you, it works perfectly. I thought I tried this before but I must have had a typo somewhere... – p0wl90 Jun 15 '15 at 13:36
0

Based on your comments, you do not have a separate .cpp file which you can use to define the storage of your static variable. In which case, you can expose a static method that declares its own static variable internally:

template <class T> class my_template : public my_class {

    protected:
        std::shared_ptr<T> sp;

        virtual bool init_impl() {
            sp = get_wp().lock();
            ...
            return true;
        }

    private:
        static std::weak_ptr<T>& get_wp()
        {
            static std::weak_ptr<T> wp;
            return wp;
        }
};

When a variable is declared static inside a function, it does not go out of scope when the function exits, it is preserved for subsequent function calls.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for your answer! I accepted the other one because it is simpler and exactly what I asked for. – p0wl90 Jun 15 '15 at 13:36