Questions tagged [using-declaration]

Use this tag for questions related to the 'using' keyword in C++.

164 questions
1224
votes
8 answers

What is the difference between 'typedef' and 'using' in C++11?

I know that in C++11 we can now use using to write type alias, like typedefs: typedef int MyInt; Is, from what I understand, equivalent to: using MyInt = int; And that new syntax emerged from the effort to have a way to express "template…
Klaim
  • 67,274
  • 36
  • 133
  • 188
31
votes
4 answers

Why are two using clauses resolving to the same type seen as ambigious in gcc

I have two base classes with using clauses class MultiCmdQueueCallback { using NetworkPacket = Networking::NetworkPacket; .... } class PlcMsgFactoryImplCallback { using NetworkPacket = Networking::NetworkPacket; .... } I then…
Andrew Goedhart
  • 937
  • 9
  • 17
26
votes
2 answers

A weird behavior of using-declaration

please see the following code struct A { using type = int; }; struct B : private A {}; struct C : B { using base_type = A; }; All of gcc 6.1, clang 3.8, and msvc 2015 update 3 refuse to compile this, as A is not an accessible name inside C since A…
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
25
votes
1 answer

using-declaration in derived class does not hide same function derived from base class

Have a look at the following code: struct A { public: virtual void f(){std::cout << "in A";}; }; struct B : A{ public: virtual void f(){std::cout << "in B";}; int a; }; struct C : B{ using A::f; void test(){f();} }; int main()…
Liu Nick
  • 349
  • 2
  • 9
19
votes
2 answers

Program with chaining of using-declarations compiles on MSVS and clang but not on GCC

Is the following program well-formed or ill-formed according to the c++ standard? namespace X { int i; } namespace Y { using X::i; } int main() { using X::i; using Y::i; } I'm getting different results with different compilers: MSVS: Compiles (…
Supremum
  • 542
  • 7
  • 23
19
votes
3 answers

using directive vs using declaration swap in C++

Please refer to the code below: #include namespace N { template class C { public: void SwapWith(C & c) { using namespace std; // (1) //using std::swap; // (2) …
ilovekonoka
  • 303
  • 1
  • 2
  • 6
18
votes
2 answers

Applying "using" keyword on C++ pure virtual function

The Class B is overriding the pure Virtual Function "print()" of class A. Class C is inheriting Class B as well as having a "using A::print" statement. Now why Class C is not an abstract class? class A { public : virtual void print()…
Gtrex
  • 247
  • 1
  • 7
18
votes
1 answer

In using-declaration, can dependent names render to constructors after template substitution?

In this example: template struct S : T { using T::X; }; T::X is a dependent name that refers to the member X in T. If S is instantiated with T = X: struct X { X(int) {} }; ... S s(42); Will the using-declaration become…
18
votes
1 answer

A using-declaration can not be repeated in function scope. Why is that?

In [namespace.udecl]/10 you have the following example: namespace A { int i; } namespace A1 { using A::i; using A::i; // OK: double declaration } void f() { using A::i; using A::i; // error: double declaration } This snippet…
Belloc
  • 6,318
  • 3
  • 22
  • 52
17
votes
4 answers

C++ using keyword

What is the difference between these two usage of using keyword: using boost::shared_ptr; and using namespace boost;
Bitmap
  • 12,402
  • 16
  • 64
  • 91
16
votes
1 answer

Trying to define namespace member via using-declaration

Consider the following program. Is it well-formed or not according to the c++ standard (references to relevant parts of the standard needed): namespace X { extern int i; } namespace N { using X::i; } int N::i = 1; int main() {} I'm getting…
16
votes
1 answer

C++ - How to introduce overload set from variadic number of bases.

The derived class hides the name of an overload set from the base class if the derived class has the same name defined, but we can always introduce that overload set back with using-declaration: template class A : public BASE { public: …
15
votes
4 answers

C++0x confusion with using declarations

What should happen for this case: struct A { void f(); }; struct B : virtual A { using A::f; }; struct C : virtual A { using A::f; }; struct D : B, C { void g() { f(); } }; The line of interest is f(). Clearly the lookup of f…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
13
votes
1 answer

Compile error when defining a member function, but only in GCC

The following program compiles without errors with MSVS, clang and GCC: class A; namespace Y { using ::A; class A {}; } int main() {} Now let's define a member function. Now it still compiles with MSVS and clang, but not with GCC: class…
Supremum
  • 542
  • 7
  • 23
13
votes
2 answers

C++ using declaration with typename in inheriting-constructors

While reading this question, I found a strange point: template class Subclass : public Baseclass { public: using typename Baseclass::Baseclass; // ^^^^^^^^ }; Since typename, Baseclass::Baseclass should be injected…
ikh
  • 10,119
  • 1
  • 31
  • 70
1
2 3
10 11