Use this tag for questions related to the 'using' keyword in C++.
Questions tagged [using-declaration]
164 questions
12
votes
1 answer
Equality of template aliases
I try to create template alias which cannot be distinguished from original.
So, I create traits to check when 2 templates (not types) are equal:
template class C1,
template class C2>
struct is_same_template…

Jarod42
- 203,559
- 14
- 181
- 302
12
votes
5 answers
using-declaration doesn't works correctly
In the following example, I'm trying to hide using Employee::showEveryDept from the last child class Designer by making it private in class Elayer -
#include
class Employee {
private:
char name[5] = "abcd";
void allDept() {…

hg_git
- 2,884
- 6
- 24
- 43
12
votes
2 answers
Is `using Base::operator T` allowed where `T` is a template type parameter?
Consider this example:
struct B { operator int(); };
template
struct X:B
{
using B::operator T;
};
GCC accepts the code, while Clang and MSVC rejects it.
Which is correct?
Note that if the base type is dependent, all the compilers…

Jamboree
- 5,139
- 2
- 16
- 36
12
votes
2 answers
Is "typename" allowed/required in C++11 using-declaration?
The following code compiles correctly in g++ and clang:
template
struct foo
{
class iterator;
using bar = foo::iterator;
};
int main() {}
however MSVC 2013 gives the following errors:
foo.cpp(9): error C2061: syntax error :…

M.M
- 138,810
- 21
- 208
- 365
12
votes
2 answers
Accessing types from dependent base classes
Does anyone know why using-declarations don't seem to work for importing type names from dependent base classes? They work for member variables and functions, but at least in GCC 4.3, they seem to be ignored for types.
template
struct…

Trevor Robinson
- 15,694
- 5
- 73
- 72
11
votes
4 answers
A 'using' declaration with an enum
A using declaration does not seem to work with an enum type:
class Sample{
public:
enum Colour {RED, BLUE, GREEN};
}
using Sample::Colour;
does not work!
Do we need to add a using declaration for every enumerators of enum type? Like…

yesraaj
- 46,370
- 69
- 194
- 251
11
votes
2 answers
C++ concepts lite and type alias declaration
Is it possible to use typedef or using to declare a type alias inside a concept, as proposed by the Concepts TS?
If I try something like the following MWE, the code does not compile (with gcc 6.2.1 and the -fconcepts switch)
#include…

Slizzered
- 869
- 2
- 9
- 23
11
votes
2 answers
'using' keyword to choose from multiple virtual inherited functions
I have a class ('TestC'), which is derived from two other classes ('TestA' and 'TestB'), both of which have a virtual function with the same signature.
To make the function accessible through 'TestC', I have to tell it which version to use. This…

Silverlan
- 2,783
- 3
- 31
- 66
11
votes
3 answers
c++ using declaration, scope and access control
Typically the 'using' declaration is used to bring into scope some member functions of base classes that would otherwise be hidden. From that point of view it is only a mechanism for making accessible information more convenient to use.
However: the…

haselhorstk
- 169
- 1
- 1
- 7
10
votes
2 answers
Namespace using declaration (bug in GCC/VS2010)?
namespace A{
int i;
}
int main(){
using A::i;
using A::i;
}
VS2010 - compiles fine
gcc (ideone) - compiles fine
Comeau - gives error ""ComeauTest.c", line 10: error: "i" has already been declared in the current scope
using…

Chubsdad
- 24,777
- 4
- 73
- 129
10
votes
1 answer
Inheriting an explicit constructor (Intel C++)
Intel C++ compiler (Version 16.0.3.207 Build 20160415) seems to drop the explicit specifier when the constructor of the base class is inherited with using. Is this a bug?
struct B
{
explicit B(int) { }
};
struct D : B
{
using B::B;
};
B b…

Evg
- 25,259
- 5
- 41
- 83
10
votes
1 answer
typedef and using declaration for same name at same scope
I scoured the C++11 standard (well, the n3242 draft) and the internet but could not find a precise answer. The code below compiles fine with clang 3.2 and g++ 4.7.2 as well as Visual Studio 2010, but I would expect to get an error instead.
#include…

stephaneyfx
- 306
- 1
- 8
9
votes
1 answer
Using declaration (Derived class)
struct B1{
int d;
void fb(){};
};
struct B2 : B1{
using B1::d;
using B1::fb;
int d; // why this gives error?
void fb(){} // and this does not?
};
int main(){}
Is it because, B1::fb() is treated as B1::fb(B1*)…

Chubsdad
- 24,777
- 4
- 73
- 129
8
votes
1 answer
Is it possible to refer to a user-defined conversion template in a using declaration?
In a class B inheriting from class A, it's possible to use a using declaration to bring members of A into B, even templates, like this:
struct A {
template
void foo();
};
struct B : private A {
using A::foo;
};
But can it…

Apples
- 3,135
- 1
- 21
- 26
8
votes
4 answers
Visibility of members of base template class not directly inherited
The access to members of a template base class requires the syntax this->member or the using directive. Does this syntax extends also to base template classes which are not directly inherited?
Consider the following code:
template
struct A…

francesco
- 7,189
- 7
- 22
- 49