I understand that the member names of a base-class template are hidden within the scope of a derived class, and therefore must be accessed using this->foo or Base<T>::foo. However, I recall that C++ also allows you to use the using keyword, which can come in handy in a derived-class function which frequently accesses a base-class variable. So, in order to avoid cluttering up the function with this-> everywhere, I'd like to use the using keyword.
I know I've done this before, but for whatever reason I can't get it to work now. I'm probably just doing something stupid, but the following code won't compile:
template <class T>
struct Base
{
int x;
};
template <class T>
struct Derived : public Base<T>
{
void dosomething()
{
using Base<T>::x; // gives compiler error
x = 0;
}
};
int main()
{
Derived<int> d;
}
The error, (with GCC 4.3) is: error: ‘Base<T>’ is not a namespace
Why doesn't this work?
It doesn't work because C++ language has no such feature and never had. A using-declaration for a class member must be a member declaration. This means that you can only use in class scope, but never in local scope. This all has absolutely nothing to do with templates.
In other words, you can place your using-declaration into class scope
struct Derived : public Base<T> {
...
using Base<T>::x;
...
};
but you can't have it inside a function.
Using-declarations for namespace members can be placed in local scope, but using-declarations for class members cannot be. This is why the error message complains about Base<T> not being a namespace.
Outside class scope (if you are in a block etc), you can only name namespace members in a using declaration.
If you don't want to place that using declaration into the scope of Derived (which IMO is the favorable solution), your other option is to use a reference
int &x = this->x;
x = 0;
It should be noted that this is semantically different, because it
Forces a definition to exist for Base<T>::x, which might not be the case for static const data members
Forces Base<T>::x to be of type int& or be convertible to type int&.
Otherwise, if you want to avoid using this-> all again, I don't see other options.
template <class T>
struct Base
{
int x;
};
template <class T>
struct Derived : public Base<T>
{
using Base<T>::x;
void dosomething()
{
x = 0;
}
};
int main()
{
Derived<int> d;
}
As others said, it is working only class scope.
Related
Inside a function one can employ the using declaration to import a name in the current scope, like
namespace A {
int y;
}
void f() { using A::y; }
A using declaration can be used in a class definition, to alter the accessibility of an inherited member, but also it is useful to explicitly bring a member inherited from a template class
template <bool activate>
struct A {
int x;
};
template <bool activate>
struct B : public A<activate> {
using A<activate>::x;
};
This is particularly useful, as it avoids the need to access to x via this->x or A<activate>::x. This can be used only inside the body of the definition, but not inside a member function.
template <bool activate>
struct A {
int x;
};
template <bool activate>
struct B : public A<activate> {
int f() const noexcept {
// This gives: "error: using-declaration for member at non-class scope"
// using A<activate>::x;
return x;
}
};
Is there a rationale for this restriction of the language, that is, for the fact that using A<activate>::x can only be placed inside the definition of the class?
Absent a direct statement on the subject in Design & Evolution of C++, it’s hard to reliably infer intent for something like this. That said, until recently, the standard described using-declarations as introducing declarations as synonyms for the named declarations. In that view, it would be more than a little strange to have a member declaration belong to a block scope. Now they are considered to be redirects that are replaced by their referents during name lookup, which would be more consistent with this notional usage.
The STL <memory> header (MSVC implementation) contains a class called:
template <class _Ty> class _Ref_count_obj2 : public _Ref_count_base
This class has a member:
union {
_Wrap<_Ty> _Storage;
};
where _Wrap is defined as:
template <class _Ty>
struct _Wrap {
_Ty _Value; // workaround for "T^ is not allowed in a union"
};
From my understanding, this code is designed to hold an object of type _Ty following its construction via the new operator. However I can't figure out why this was done; it seems like using a struct instead of a struct inside a union would work just as well.
Can anyone explain the reasoning behind this? Also, can anyone explain the comment in the _Wrap definition?
First, embedding the _Storage member in a union will prevent default destruction of that object (which is, more than likely, a non-trivial type); this appears to be essential, as the class involved is a reference counter. (By default, unions have a deleted destructor; see, for example: Is a Union Member's Destructor Called .)
Second, using an anonymous union 'moves' the _Storage identifier into the enclosing scope, thus removing any need for X.-style notation (if the union were to be named X). From cppreference:
Members of an anonymous union are injected in the enclosing scope (and
must not conflict with other names declared there).
Last, the need to wrap the union's member into a templated structure is so that the class will work with reference types, which are not allowed in unions. To check this last part out, try the following code with the commented-out lines made active:
template <class _Ty>
struct _Wrap {
_Ty _Value; // workaround for "T^ is not allowed in a union"
};
template<class T>
class bob {
public:
bob(T t) : uncle{t}//, aunty(t)
{
}
private:
union {
_Wrap<T> uncle;
};
// union {
// T aunty;
// };
};
int main()
{
int i = 42;
bob<int&> b{ i }; // Note: Template uses a REFERENCE type
return 0;
}
I'm reading Effective C++ 3rd Edition, item 43 "Know how to access names in templatized base classes".
template<typename T>
class B {
T i;
};
template<typename T>
class D: public B<T> {
public:
void Foo() {
T a = B<T>::i;
}
};
int main() {
D<int> d;
}
For the above codes, I know if the B<T>:: is not added before i in D::Foo(), compilers will complain "i was not declared in this scope". (But it didn't complain i is private in B.)
However, if T i; is not declared in B, like the following, the compiling goes well.
template<typename T>
class B {
};
template<typename T>
class D: public B<T> {
public:
void Foo() {
T a = B<T>::i;
}
};
int main() {
D<int> d;
}
Compilers are defaulted not to find names in templatized base classes.
But why they still don't do even I told them?
But why they still don't do even I told them?
Because the member function Foo is not used, then it's not instantiated at all.
This applies to the members of the class template: unless the member is used in the program, it is not instantiated, and does not require a definition.
You might get an error if Foo is called, like
D<int> d;
d.Foo();
BTW
But it didn't complain i is private in B.
Because accessibility check is performed after name lookup. The name i is not found, then accessibility of nothing could be checked.
I am apparently misunderstanding how the C-ish feature of simultaneously "declaring" and referring to a type using struct X works in C++. (Plus I don't even know what this feature is called, properly.) I have three examples below to show what I'm doing but the main question is: Why is the nested type, forward declared in this way, at global scope instead of in class scope?
This works to forward declare a nested struct:
#include <memory>
class Foo1
{
Foo1();
~Foo1();
struct Impl;
std::auto_ptr<Impl> m_pimpl;
};
struct Foo1::Impl
{
Impl(){}
~Impl(){}
};
Foo1::Foo1(){}
Foo1::~Foo1(){}
int main() {}
So now I want to save a few characters by using a half-remembered C feature, so see where I've dropped the line struct Impl; and for the field declared its type as auto_ptr<struct Impl> instead of auto_ptr<Impl>:
#include <memory>
class Foo2a
{
Foo2a();
~Foo2a();
std::auto_ptr<struct Impl> m_pimpl;
};
struct Foo2a::Impl
{
Impl(){}
~Impl(){}
};
Foo2a::Foo2a(){}
Foo2a::~Foo2a(){}
int main() {}
Here the compiler complains that no struct named 'Impl' in 'Foo2a'. Sure enough, it is at global scope, as this compiles:
#include <memory>
class Foo2b
{
Foo2b();
~Foo2b();
std::auto_ptr<struct Impl> m_pimpl;
};
struct Impl
{
Impl(){}
~Impl(){}
};
Foo2b::Foo2b(){}
Foo2b::~Foo2b(){}
int main() {}
What's up with that? a) Why does it work at all to "declare" the type Impl this way, and b) given that it works, why is Impl at global scope not class scope?
And, BTW, declaring the field as auto_ptr<struct Foo2c::Impl> doesn't work either.
The scope of a name first declared as elaborated-type-specifier depends on the way in which it is used.
If you do struct Impl; the struct is declared as a member of whatever scope that declaration occurs in, as in your first example. But in any other context, it's declared in the nearest enclosing block or namespace (but not class), as in the second and third examples.
The solution is just to forward-declare it as struct Impl; in the class prior to any other uses. You can just refer to it as Impl after that.
See [basic.scope.cdecl]/7 for the detailed rules.
I can't get this to compile at all. I may not be possible but I don't know why it should not be.
class A {
template <typename T>
class B {
int test() { return 0; }
};
//template <> class B<int>; <-with this, namepace error
B<int> myB_;
};
template <> class A::B<int> {
int test() {
return 1;
}
};
As it appears the compiler complains "The explicit specialization "class A::B" must be declared before it is used."
If I try to provide the froward declaration in the commented line, the compiler complains
"The explicit specialization "B" must be declared in the namespace containing the template."
We use 2 different compilers here. This error is from IBM's "xl" compiler on AIX but I get similar errors with different verbage when compiling on our Sun systems.
It seems like a catch-22.
Obviously, this is a highly contrived, simplistic example but, it represents the problem.
I want to define a template class within a class because the template class is only relevent to the containing class. There should be no access to the template from outside the class.
Am I missing something?
You are correct. This is not possible to do (as far as I know). Your member declaration causes an implicit instantiation before the explicit specialization was declared. But how would you want to declare it? You cannot do that in class scope. Others have felt that this is an ugly restriction.
You could work around this by making the class member a pointer. This would not need to implicitly instantiate the class at that point, but rather at the point where you create the object in the end. I realize that this is an ugly work around. So better find other ways to do this.
For instance partial specializations are allowed in class scope. So you could add a dummy template parameter, then you can specialize this in the class before the member declaration. Likewise, i find this ugly, but it would not disturb things that much, I feel.
You could work around the issue by using an unnamed namespace for privacy:
namespace {
template <typename T>
class B {
int test() { return 0; }
};
template <> class B<int> {
int test() {
return 1;
}
};
}
class A {
B<int> myB_;
};
This will compile, but if A needs to be visible outside this compilation unit, you'll need more elaborate machinery (e.g., interface and factory or Pimpl).
B is not a template class and you are trying to specialize it. That is the cause for the error. You can check these two errors C2913 and C3413.
Is this what you are looking for?
class A
{
template<class T>
class B
{
inline int test()
{
return 0;
}
};
A::B<int> myB_;
};