using declaration inside a member function for an inherited member field - c++

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.

Related

Why would one wrap a struct in an anonymous union? (STL msvc implementation)

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;
}

Why do the usual access control checking applies to names used to specify explicit instantiation when accessed through template parameters?

The C++11/14 standards state the following in note 14.7.2/12 [temp.explicit]:
The usual access checking rules do not apply to names used to specify
explicit instantiations. [ Note: In particular, the template arguments
and names used in the function declarator (including parameter types,
return types and exception specifications) may be private types or objects
which would normally not be accessible and the template may be a member
template or member function which would not normally be accessible. — end note ]
I would expect to be allowed to use a template if I can instantiate it.
I tried with gcc-4.8.2 and I get the expected behaviour when I access private members of explicitly named classes. However, the access checking rules do apply when I access private members through template parameters. Is this a bug in gcc, or am I missing something?
In the code below, the only difference between 'succeeds' and 'fails' is that the former accesses the private member directly via 'A', while the latter accesses it via the template parameter 'T'. The compiler complains that privateFoobar is private in that context.
#include <iostream>
#include <string>
struct A
{
private:
std::string privateFoobar() {return "private foobar!";}
};
typedef std::string (A::*Foobar)();
template <class Type, Type value>
struct Access
{
static Type getValue() {return value;}
};
template <class T>
struct IndirectAccess
{
static Foobar succeeds() {return Access<Foobar, &A::privateFoobar>::getValue();}
static Foobar fails() {return Access<Foobar, &T::privateFoobar>::getValue();}
};
template class Access<Foobar, &A::privateFoobar>;
int main() {
std::cout << (A().*Access<Foobar,&A::privateFoobar>::getValue())() << std::endl;
std::cout << (A().*IndirectAccess<A>::succeeds())() << std::endl;
std::cout << (A().*IndirectAccess<A>::fails())() << std::endl;
}
In case you are wondering what is the use case for such a sackable offence: creating a framework that would automate the configuration of an application based on implementation choices for the selected components.
Explicit instantiations have to be at namespace scope, which means private members of classes would not normally be accessible. Without the rule you quote, this would be impossible:
class Foo
{
private:
struct Bar;
template<typename T> class Baz { };
public:
void f(); // does things with Baz<Bar>
};
// explicit instantiation declaration
extern template class Foo::Baz<Foo::Bar>;
Without that rule, I would not be able to name Foo::Bar or even Foo::Baz at namespace scope, because those names are private to Foo.
Since I'm not actually using Foo::Bar or Foo::Baz here, just referring to their names to tell the compiler I'm instantiating the template somewhere else, there is no real access violation (although it is possible to use this rule to perform a very sneaky trick not possible otherwise).
Similarly, when I write the explicit instantiation definition in some other file, I need to be able to refer to the private names again at namespace scope.

"Using" directive fails within template

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.

What can I "forward declare" in C++?

I know I can do
class Foo;
and probably
struct Bar;
and global functions
bool IsValid(int iVal);
What about a typed enum? What about a typed enum within an undeclared class? What about a function with an undeclared class? What about a static member within an undeclared class? What about these within an unknown namespace? Am I missing anything else that can be forward declared?
You can forward declare
Templates, including partial specializations
Explicit specializations
Nested classes (this includes structs, "real" classes and unions)
Non-nested and local classes
Variables ("extern int a;")
Functions
If by "forward declaration" you strictly mean "declare but not define" you can also forward declare member functions. But you cannot redeclare them in their class definition once they are declared. You cannot forward-declare enumerations. I'm not sure whether I missed something.
Please note that all forward declarations listed above, except partial and explicit specializations, need to be declared using an unqualified name and that member functions and nested classes can only be declared-but-not-defined in their class definition.
class A { };
class A::B; // not legal
namespace A { }
void A::f(); // not legal
namespace A { void f(); } // legal
class B { class C; }; // legal
class B::C; // declaration-only not legal
class D { template<typename T> class E; };
template<typename T> class D::E<T*>; // legal (c.f. 14.5.4/6)

Declaring an instance of an explicit specializtion of a template within a regular class

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_;
};