c++ static or non-static templated member functions [duplicate] - c++

This question already has answers here:
static template functions in a class
(2 answers)
Closed 6 years ago.
I have a class MyClass
class MyClass
{
template <std::size_t N>
void MyFunc() { // Do something with N };
};
Then, is MyFunc a static or non-static member function?

It's a non-static member function template. If you declared it with static then it would be a static member function template.
MyFunc<0> is a non-static member function, so you would call it like so (assuming that you made it public):
MyClass c{};
c.MyFunc<0>();

Related

Derived class cannot see base class member [duplicate]

This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Closed 2 years ago.
What am I doing wrong in this code?
template <typename T>
class CLASS1
{
public:
T member;
};
template <typename T>
class CLASS2 : public CLASS1<T>
{public:
void func()
{
member = 4;
}
};
Visual Studio error:
member identifier not found
G++ error:
‘member’ was not declared in this scope
You need to specify where the name member comes from. In this case, it comes from the inherited class template CLASS1<T>, so you need to say:
void func()
{
CLASS1<T>::member = 4;
}
If you say this->member, then the compiler knows to look for names in the base classes as well. So you could do:
void func()
{
this->member = 4;
}

static variable instantiation outside class [duplicate]

This question already has answers here:
Why static variable needs to be explicitly defined?
(5 answers)
Closed 2 years ago.
So I have this file
template <typename T>
class TestStatic {
public:
static int staticVal;
};
//static member initialization
template<typename T> int TestStatic<T>::staticVal;
I don’t understand why I have to instantiate the Staticval isn’t it already instantiated in the class definition? Also does it generate a static variable for each template parameter types?
Thanks in advance.
This line:
static int staticVal;
inside the class is a declaration, not a definition. That's why you have to define it outside the class like this:
template<typename T>
int TestStatic<T>::staticVal = 0;
And yes, this will generate a definition of the member for all types T.
Alternatively, you could define the static variable inline, like this:
template <typename T>
class TestStatic {
public:
inline static int staticVal = 0;
};
which has the same semantics as above, but let's you avoid having to define the static variable separately outside the class.
As the variables declared as static are initialized only once as they are allocated space in separate static storage so, the static variables in a class are shared by the objects. There can not be multiple copies of the same static variables for different objects. Also because of this reason static variables can not be initialized using constructors.
Please refer for more information: Reference

Is it possible to access a member inside the parent class body with CRTP? [duplicate]

This question already has answers here:
invalid use of incomplete type
(5 answers)
Closed 4 years ago.
Is there a way in C++11/14/17 to access a member of the subclass in the parent class when using CRTP?
template <typename T>
class A {
public:
using C = typename std::result_of<decltype(&T::next)(T)>::type;
};
class B : A<B> {
public:
int next() { ... };
};
This should result in A<B>::C and B::C being int.
No, I am afraid that is not possible. When A<B> is used as the base class, it must be instantiated (since a base class must be complete), but at that point B is still an incomplete type and thus its members cannot be accessed inside A<B>.

Sometimes class members are not inherited by subclasses [duplicate]

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Derived template-class access to base-class member-data
(3 answers)
Closed 5 years ago.
When we create a template class example1 which derives from another template class example2. The template class example1 does not inherit systematically of example2 members.
Consider the code bellow:
template<class T>
class example2 {
protected:
int a;
public:
int getA() const {return a;}
};
template<class T>
class example1 : public example2<T> {
T* p;
public:
void test() {
example2<T>::a = 8;
}
};
in the method test of the class example1, we have to use example2<T>:: operator in order to be able to access to the member a. Otherwise, the complier generates an error: 'a' was not declared in this scope.
I mean, why the complier does not recognize a in the class example1? Why we should specify example2<T>::a and not just a?
Thanks

Why is the compiler not generating code for my uninstantiated template class's static member variable? [duplicate]

This question already has answers here:
C++ Static member initialization (template fun inside)
(3 answers)
How to force a static member to be initialized?
(6 answers)
Closed 9 years ago.
This question is hard to word, primarily because of the terms class instantiation vs. template instantiation. I have a template class that is full of static functions and members. Each specialization of this template needs to have some initialization done before it's first use.
My initial plan was to just give the template class a static initializer member, which will initialize the template class's static members during it's construction at dynamic initialization.
However, this isn't working. Unless I explicitly invoke code in the initializer class, the compiler won't generate any code or storage for it.
For example:
template<typename Tag>
class WorkerPool
{
struct WorkerInitializer
{
void foo() { }
WorkerInitializer() { WorkerPool<Tag>::start(); }
};
friend struct WorkerInitializer;
static WorkerInitializer _initializer;
static void start() { std::cout << "Started" << std::endl; }
public:
static void async() { std::cout << "Async" << std::endl; }
};
template<typename T> typename WorkerPool<T>::WorkerInitializer
WorkerPool<T>::_initializer;
struct GenericWorker { };
int main()
{
WorkerPool<GenericWorker>::async();
}
The output is just 'Async'.
However, if I change it so that calling WorkerPool<T>::async() calls _initializer.foo(), then the initializer does get compiled and correctly constructed, as I would expect.
Why is the compiler refusing to generate code for my static member?
I've tested with Visual Studio 2010/2012, gcc, and clang, and all produce the same result; my static member is not constructed. This makes me think that the standard requires this behavior, but I'm having trouble understanding why this might be.