Both clang ++ and g ++ complain about the following code:
#include <iostream>
template<unsigned N>
class A {
public:
int k;
void printk() {std::cout << k << std::endl;}
A() : k(0) {}
};
template<unsigned N>
class B : public A<N> {
public:
B() {
this -> k ++;
A<N> :: k ++;
}
};
int main () {
B<1>().printk();
}
If I do not declare Bas a template, I can use kwithout additional qualifications:
#include <iostream>
template<unsigned N>
class A {
public:
int k;
void printk() {std::cout << k << std::endl;}
A() : k(N) {}
};
class B : public A<0> {
public:
B() {
this -> k ++;
A<0> :: k ++;
k ++;
}
};
int main () {
B().printk();
}
Or, if I do not declare A as a template:
#include <iostream>
class A {
public:
int k;
void printk() {std::cout << k << std::endl;}
A() : k(0) {}
};
template<unsigned N>
class B : public A {
public:
B() {
this -> k += N;
A :: k += N;
k += N;
}
};
int main () {
B<1>().printk();
}
Why? (I assume this is not a mistake, since both compilers behave the same.) Should it be like that?
source
share