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.
Related
This question already has answers here:
template base class typedef members invisible
(1 answer)
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 2 years ago.
I have the following code which works fine:
template<typename T>
struct Foo
{
using Bar = int;
};
struct Baz : public Foo<Baz>
{
Bar bar;
};
Now I want to change Baz to accept a template parameter itself.
template<typename T>
struct Foo
{
using Bar = int;
};
template<typename T>
struct Baz : public Foo<Baz<T>>
{
Bar bar;
};
This fails to compile in every compiler with error: unknown type name 'Bar'. In fact, even if I put using Foo<Baz>::Bar; above that line, it still fails. (Yet the using itself compiles file!) Why?
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 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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?
#include <iostream>
using namespace std;
template <class T>
class Test
{
union obj
{
union obj* next;
int num;
};
static const int SZ=3;
static obj* volatile list[SZ];
};
template <class T>
Test<T>::obj* volatile
Test<T>::list[SZ]=
{
0, 0, 0
};
int main()
{
return 0;
}
With g++, the error I get is:
18|error: expected constructor, destructor, or type conversion before '*' token
Add the keyword typename before Test<T>::obj* in the definition of the member.