This question already has answers here:
Why doesn't a derived template class have access to a base template class' identifiers?
(4 answers)
Closed 7 years ago.
The following code
template<int c>
struct Base
{
static const int a = c + 5;
};
template<int c>
struct Derived : Base<c>
{
static const int b = a + 5;
};
... fails to compile because a was not declared in this scope. Explicitly specifying Base<c>::a works, but logically this shouldn't be necessary because we're deriving from Base<c>. Is this intended behaviour (and why) or am I missing something?
template<int c>
struct Derived : Base<c>
{
static const int b = Base<c>::a + 5;
};
It really depends on compiler, but gcc requires it, it plays stupid when it comes to template classes
Related
This question already has answers here:
Accessing type aliases in base class from derived class [duplicate]
(1 answer)
Why can't I use alias from a base class in a derived class with templates?
(3 answers)
Closed 13 days ago.
I'm working with some classes which are expected to perform as a singleton. My code works like this:
template <typename T>
struct Singleton {
public:
using SingletonType = T;
protected:
static T& getInstance() {
static T instance;
return instance;
}
};
struct NormalClass: protected Singleton<NormalClass> {
//The following three seem to be the same
//friend class Singleton;
//friend class Singleton<NormalClass>;
friend class Singleton<SingletonType >;
static const SingletonType& instance() {return Singleton<SingletonType>::getInstance();}
protected:
NormalClass();
};
But when I work with a template class, it forces me to explicitly declare the typename for Singleton and I cannot access Singleton::SingletonType
template <int N>
struct TemplateClass: protected Singleton<TemplateClass<N>>{
friend class Singleton<TemplateClass>; // It works fine
//friend class Singleton<SingletonType >; //error: 'SingletonType' was not declared in this scope;
protected:
int n;
TemplateClass():n(N){}
};
Could someone explain why to me? Thanks for your answer.
This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Why doesn't a derived template class have access to a base template class' identifiers?
(4 answers)
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 3 years ago.
Why using std::conditional prevents a child struct to access public members of a parent struct? As a case in point, the following program doesn't compile, complaining that a is not defined. What would be the right way to fix this?
template <int i>
struct mtmd_A {
static constexpr int a = i;
};
template <int i>
struct mtmd_B {
static constexpr int a = i * i;
};
template <bool select>
struct mtmd: public std::conditional<select, mtmd_A<17>, mtmd_B<17>>::type {
static constexpr int b = a;
};
int main()
{
std::cout << mtmd<true>::b << std::endl;
}
Edit
This doesn't seem to be a duplicate of this question since the problem appears only when I add std::conditional.
This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 5 years ago.
I'm having the weirdest error and I've no idea what I'm doing wrong.
template <bool X>
struct A {
int x;
};
template <bool X>
struct B : public A<X> {
B() { x = 3; } // Error: 'x' was not declared in this scope.
};
I don't understand how it's possible that I cannot see x from B, given that I'm inheriting A publicly.
At the same time, this code compiles:
template <bool X>
struct A {
int x;
};
template <bool X>
struct B : public A<X> {};
int main() {
B<false> b;
b.x = 4;
};
I'm compiling with g++ 7.0.1.
Edit: It seems that if I reference the full-name of x, the code compiles, as in:
B() { A<X>::x = 3; }
But why?
The compiler doesn't know how to find x. Use this->X and it will work.
At the time the compiler is doing its first pass over struct B, the template parameter is not known. By using the this pointer, you are deferring the name lookup until the second pass.
This question already has answers here:
How does the compilation of templates work?
(7 answers)
Closed 5 years ago.
what happen when a class template instantiate . i.e i hv a class template and i have created class for int and class for float, so what will happen at compile time( compiler will create 2 seprate class for int and float) or not?
eg :
template <typename T>
class A
{
public:
void foo(T t)
{
//...
};
};
int main()
{
A<int> a;
A<float> b;
}
Yes, the compiler will create two new classes on-the-fly - one for int and one for float.
This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
problem with template inheritance
This code doesn't compile in GCC:
template <typename T>
struct Base
{
public:
int x;
};
template <typename B>
struct Middle : public B
{
};
template <typename T>
struct Sub : public Middle<Base<T> >
{
public:
void F()
{
x=1; // error: ‘x’ was not declared in this scope
}
};
If either Base or Sub weren't template classes, it wouldn't complain. VC handles it.
Why?
Use this->x = 1; to tell the compiler that x is a (template-) dependent name.
Note: What GCC does ot fine according to the standard, MSVC is just a bit more tolerant.