using-declaration

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

别等时光非礼了梦想. 提交于 2019-12-31 19:23:51
问题 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. 回答1: 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

Does Argument-Dependent Lookup go before normal scope lookup?

牧云@^-^@ 提交于 2019-12-23 17:33:53
问题 This is the code in question that appears in §13.3 of "C++ Primer", 5ed: void swap(Foo &lhs, Foo &rhs) { using std::swap; swap(lhs.h, rhs.h); // uses the HasPtr version of swap // swap other members of type Foo } The book mentions the phenomenon of the class-specific swap not being hidden by the using declaration, and refers reader to §18.2.3: I read that section and realized that this may be related to Argument-Dependent Lookup (ADL). The following is the excerption: But I still have some

Is a using-declaration supposed to hide an inherited virtual function?

旧巷老猫 提交于 2019-12-23 08:04:33
问题 struct level0 { virtual void foo() = 0; }; struct level1 : level0 { virtual void foo() { cout <<" level1 " << endl; } }; struct level2 : level1 { virtual void foo() { cout <<" level2 " << endl; } }; struct level3 : level2 { using level1::foo; }; int main() { level1* l1 = new level3; l1->foo(); level3 l3; l3.foo(); return 0; } the above code using gcc gives level2 level1 but in icc gives level2 level2 Which one is correct or is it undefined by standard? Edit: This proves there is a bug for

Redefinition inconsistency in clang between struct and int

大城市里の小女人 提交于 2019-12-22 11:30:52
问题 The following program gives no error when compiling with clang: namespace X { struct i {}; } namespace Y { using X::i; struct i {}; } int main() {} Let's use int instead of struct, then we get: namespace X { int i; } namespace Y { using X::i; int i; } int main() {} This program gives a redefinition error when compiling with clang. The only difference between the programs is the kind of entity used (struct or int), but one compiles without errors and the other gives a redefinition error. Does

Equality of template aliases

家住魔仙堡 提交于 2019-12-22 04:10:11
问题 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 <template <class...> class C1, template <class...> class C2> struct is_same_template : std::false_type {}; template <template <class...> class C1> struct is_same_template<C1, C1> : std::true_type {}; Now test it: // Expected alias template <typename ... Ts> using V_Ts = std::vector<Ts...>; // Variadic // Fallback alias template <typename T,

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

北城余情 提交于 2019-12-18 18:55:25
问题 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

c++ using declaration, scope and access control

ぐ巨炮叔叔 提交于 2019-12-17 18:56:36
问题 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 'using' declaration can also be used to change access constraints (not only for functions but also for attributes). For example: class C{ public: int a; void g(){ cout << "C:g()\n"; } C() : a(0){} }; class D : public C{ private: using C::a; using

Using declaration as overrider

左心房为你撑大大i 提交于 2019-12-13 13:07:03
问题 We have the following simple (and slightly modified to add main and output) example in the Standard: struct A { virtual void f() { cout << "A\n"; } }; struct B : virtual A { virtual void f() { cout << "B\n"; } }; struct C : B, virtual A { using A::f; }; int main() { C c; c.f(); // calls B​::​f, the final overrider c.C::f(); return 0; } From which we can make a conclusion that using A::f does not present an overrider. But what wording in the Standard dictates it? Here is the wording for the

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

空扰寡人 提交于 2019-12-11 11:56:51
问题 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

Example of error caused by using directive in namespaces

帅比萌擦擦* 提交于 2019-12-11 06:54:24
问题 I'm trying to understand what kind of errors could arise from including using declarations in namespaces. I'm taking into account these links. I'm trying to create an example where an error is being caused by a name being silently replaced by an header file being loaded before another one, due to usage of the using declaration. Here I'm defining MyProject::vector : // base.h #ifndef BASE_H #define BASE_H namespace MyProject { class vector {}; } #endif This is the "bad" header: here I'm trying