using-declaration

Accessing types from dependent base classes

断了今生、忘了曾经 提交于 2019-12-05 00:56:58
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 <class T> struct Base { typedef T value_type; }; template <class T> struct Derived : Base<T> { // Version 1: error on conforming compilers value_type get(); // Version 2: OK, but unwieldy for repeated references typename Base<T>::value_type get(); // Version 3: OK, but unwieldy for many types or deep inheritance typedef typename Base<T>::value_type value_type; value

Using-declaration of an existing namespace type vs creating a type alias

余生长醉 提交于 2019-12-04 07:02:52
This is not a question about the difference between using and typedef for creating type aliases. I would like to provide access to an existing type from a namespace inside a code block or a function. I found two different ways : I can "include" the type with a using declaration : using typename mynamespace::mytype; Or I can create a type alias : typedef mynamespace::mytype mytype; using mytype = mynamespace::mytype; //C++11 Is there any difference ? What are the pros and cons of each syntax ? Which one is the most used/recommended ? Thank you. Related question : Using-declaration of an

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

荒凉一梦 提交于 2019-12-03 15:08:31
问题 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 ( http://webcompiler.cloudapp.net/ ) Clang: Compiles ( http://melpon.org/wandbox/permlink/KloDufJ5h1DalK4v ) GCC: Compile error ( http://melpon.org/wandbox/permlink/IKuuQGE1THofuUTr ) I don't want to fix this program to make it compile on GCC. I just

C++ using keyword

ⅰ亾dé卋堺 提交于 2019-12-03 11:03:34
What is the difference between these two usage of using keyword: using boost::shared_ptr; and using namespace boost; Alok Save using boost::shared_ptr; Includes only the shared_ptr from the boost namespace in your current namespace. This means you can use the shared_ptr without qualifying it with namespace boost . It is called a using declaration . using namespace boost; Includes all the symbols in the boost namespace in your current scope. This means you can use all the symbols in the boost namespace without qualifying them with namespace boost . It is called as using directive . Why should

using directive vs using declaration swap in C++

岁酱吖の 提交于 2019-12-03 09:04:46
问题 Please refer to the code below: #include <algorithm> namespace N { template <typename T> class C { public: void SwapWith(C & c) { using namespace std; // (1) //using std::swap; // (2) swap(a, c.a); } private: int a; }; template <typename T> void swap(C<T> & c1, C<T> & c2) { c1.SwapWith(c2); } } namespace std { template<typename T> void swap(N::C<T> & c1, N::C<T> & c2) { c1.SwapWith(c2); } } As written above, the code doesn't compile on Visual Studio 2008/2010. The error is: 'void N::swap(N::C

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

妖精的绣舞 提交于 2019-12-03 02:45:24
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 compiles in clang. The first is a declaration inside a namespace, and the multiple using statements could happen frequently using #includes. The second is inside a definition of a function, and you would never do that unless you made a mistake. You can't define the same symbol twice either, for example, but you can declare several times. The using statement is also more

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

瘦欲@ 提交于 2019-11-30 17:58:21
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 BASE> class A : public BASE { public: using BASE::some_method; void some_method(); } But what if I introduce all overload sets from variadic base classes? Would I be able to write something like this? template <class... BASES> class A : public BASES... { public: using BASES::some_method...; void some_method(); } I've considered using a helper class like: template <class... BASES> struct

Overload resolution between template members in base and derived classes

不想你离开。 提交于 2019-11-30 15:00:23
Microsoft compiler (Visual Studio 2017 15.2) rejects the following code: #include <type_traits> struct B { template<int n, std::enable_if_t<n == 0, int> = 0> void f() { } }; struct D : B { using B::f; template<int n, std::enable_if_t<n == 1, int> = 0> void f() { } }; int main() { D d; d.f<0>(); d.f<1>(); } The error is: error C2672: 'D::f': no matching overloaded function found error C2783: 'void D::f(void)': could not deduce template argument for '__formal' note: see declaration of 'D::f' Clang also rejects it: error: no matching member function for call to 'f' d.f<0>(); ~~^~~~ note:

Namespace using declaration (bug in GCC/VS2010)?

自闭症网瘾萝莉.ら 提交于 2019-11-30 08:56:23
问题 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 A::i;" $7.3.3/8 - "A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed." The example right there indicates that the code is indeed ill-formed. So, is this a bug in GCC and VS2010? EDIT 2: Remove

Namespace using declaration (bug in GCC/VS2010)?

ε祈祈猫儿з 提交于 2019-11-29 09:28:50
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 A::i;" $7.3.3/8 - "A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed." The example right there indicates that the code is indeed ill-formed. So, is this a bug in GCC and VS2010? EDIT 2: Remove the multiple using directives as it was unrelated to the query on hand. The example you refer to is