name-lookup

Ambiguous name lookup with using-directive

眉间皱痕 提交于 2019-12-19 16:32:12
问题 It's not allowed to put a namespace and a class with the same name into one declarative region, i.e. namespace A {} class A{}; is ill-formed (see §3.3.1/4). However, one can introduce the name of either one via a using-directive: namespace N { namespace A {int i;} } struct A {static int i;}; using namespace N; int i = A::i; // The global struct, or namespace N::A? Is this code ill-formed? VC++ thinks so, as well as Clang: main.cpp:7:9: error: reference to 'A' is ambiguous int i = A::i; ^ main

Multiple Inheritance Template Class

假装没事ソ 提交于 2019-12-19 06:29:19
问题 class messageA { }; class messageB { }; template<class T> class queue { public: virtual ~queue() {} void submit(T& x) {} }; class A : public queue<messageA>, public queue<messageB> { }; int main() { A aa; aa.submit(messageA()); aa.submit(messageB()); } My first thought is, the above code should be fine, as class A will contains 2 overloaded submit functions, which will accept messageA and messageB object. However, the compiler gives me the following error : May I know why there is an

namelookup with Unqualified name : C++0x draft n3290

风流意气都作罢 提交于 2019-12-19 03:23:13
问题 A point from the ISO C++ Draft n3290 : 3.4.0 2nd point A name “looked up in the context of an expression” is looked up as an unqualified name in the scope where the expression is found. Would someone please explain this statement with an example? 回答1: It says that the scope which contains the expression will be searched for the name. i.e. namespace foo { struct bar { void foobar() { do_something(); } }; } if you have this code the name do_something will be searched in the scope of foobar ,

Unqualified name lookup: Why local declaration hides declaration from using directive

a 夏天 提交于 2019-12-18 08:31:22
问题 Consider this code: namespace A { int i = 24; } namespace B { using namespace A; int i = 11; int k = i; // finds B::i, no ambiguity } And basic.lookup.unqual.2: §6.4.1 Unqualified name lookup [basic.lookup.unqual] The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace

Why class member functions shadow free functions with same name?

ε祈祈猫儿з 提交于 2019-12-18 05:50:08
问题 It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely I mean that every free function with the same name is not considered for overload resolution at all. I can understand why it's done with something like this: void f(); struct S { void f(); void g() { f(); // calls S::f instead of ::f } }; where the functions have identical signatures, its only natural as variable scoping works the same way. But

C++ detecting free function existence with explicit parameters

谁说我不能喝 提交于 2019-12-18 04:11:08
问题 I'm writing some type traits to see if a free function exists with a specific set of parameters. The functions have a signature that looks something like this: template <class T> void func( SomeClass &, SomeType const & ); I know ahead of time the values for T , SomeClass , and SomeType . I want the trait to return true if this function exists with exactly these parameters, not using any implicit conversion. I can easily write some code to detect whether this function exists by using SFINAE

Is ADL the only way to call a friend inline function?

一笑奈何 提交于 2019-12-18 01:52:52
问题 Let us define f , as a friend function of S , inside the declaration of S : struct S { friend void f() {} }; I cannot find a way to call f . Is it true, then, that such an inline friend function can only be called with argument-dependant lookup? struct S { friend void f() {} friend void g(S const&) {} } const s; int main() { // f(); // error: 'f' was not declared in this scope // S::f(); // error: 'f' is not a member of 'S' g(s); // S::g(s); // error: 'g' is not a member of 'S' } Bonus: what

Special behavior for decltype of call operator for incomplete types

白昼怎懂夜的黑 提交于 2019-12-13 11:47:38
问题 I've been struggling with a compilation issue, and have been able to shrink the problem down to a small code segment. To set the stage, I'm trying to do CRTP, where the base method calls another in the derived class. The complication is, I want to use trailing return types to get the type of forwarding directly to the the Derived class's method. This always fails to compile unless I forward to the call operator in the derived class. This compiles: #include <utility> struct Incomplete;

How can I get the name of a function from a symbol in clojure?

爱⌒轻易说出口 提交于 2019-12-12 15:21:11
问题 Suppose I define x as symbol function foo (defn foo [x] x) (def x foo) Can the name "foo" be discovered if only given x? Is there a way within foo to look up the name of the function x - "foo" in this case? (foo x) Is there or is it possible to create a function such as: (get-fn-name x) foo 回答1: A similar question was asked recently on this site; see here When you do (def x foo) , you are defining x to be "the value at foo ", not " foo itself". Once foo has resolved to its value, that value

Why does the size of the same identifier differ in C and C++?

微笑、不失礼 提交于 2019-12-12 12:23:12
问题 #include <stdio.h> int T; int main() { struct T { double x; }; printf("%zu", sizeof(T)); return 0; } If I run this code in C, the result is 4 , while in C++ it is 8 . Can someone explain why the difference? 回答1: Short answer: Because they aren't the same identifier, in fact. In C, structure names and variable names fall into different namespaces, so in C, sizeof(T) == sizeof(int) // global variable T sizeof(struct T) == sizeof(struct T) // not the same namespace In C++, however, structure