Assuming the following example:
#include <iostream>
#include <mutex>
template< typename S >
class Base
{
public:
Base () { std::cout << "hello" << std::endl; }
virtual S get () { return S(5); }
protected:
std::mutex mtx;
};
template< typename S >
class Derived : public Base< S >
{
public:
using Base< S >::Base;
virtual void lock () { std::unique_lock<std::mutex> lck(mtx); } // compilation error
};
int main ()
{
Derived< int > d; // hello
std::cout << d.get() << std::endl; // 5
d.lock();
return 0;
}
Why g++
compiler "can't see" member variable mtx
??
$ g++ -Wall -g -std=c++11 -o inher main.cpp
main.cpp: In member function ‘virtual void Derived<S>::lock()’:
main.cpp:19:65: error: ‘mtx’ was not declared in this scope
virtual void lock () { std::unique_lock<std::mutex> lck(mtx); }
^
Even if mtx
is declared protected in Base
using public
inheritance? According to http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/ and others, derived class has access to protected Base members.
Is it some template issue? How can I access protected members of Base class? Thanks for hints.
Note: example does not work even if I use int
instead of std::mutex