argument-dependent-lookup

template object's template friend functions and namespaces

一世执手 提交于 2021-02-07 06:13:05
问题 In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get<int, int, float, double> : template<typename ...Args> class obj { bool p = false; template<typename T, typename... Args2> friend T get(const obj<Args2...> &o) { return o.p; } }; template<typename T, typename... Args> T get(const obj<Args...> &o); bool test(const obj<int,

template object's template friend functions and namespaces

家住魔仙堡 提交于 2021-02-07 06:09:57
问题 In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get<int, int, float, double> : template<typename ...Args> class obj { bool p = false; template<typename T, typename... Args2> friend T get(const obj<Args2...> &o) { return o.p; } }; template<typename T, typename... Args> T get(const obj<Args...> &o); bool test(const obj<int,

Should this function call be ambiguous?

独自空忆成欢 提交于 2021-01-27 04:56:30
问题 I stumbled on this the other day and can't figure out which answer is correct, or whether both are acceptable. Specifically, I'm referring to the call to bar(T{}) in OtherFunction. From what I've been able to test on compiler explorer, the decision seems split. msvc and icc agree that it is ambiguous while gcc and clang compile the code without issue. The function bar inside the namespace hidden becomes visible through argument-dependent lookup. Additionally, msvc/icc consider the declaration

Two-phase function template compilation: not *only* ADL is employed in the 2nd phase?

旧时模样 提交于 2020-06-14 06:33:47
问题 I'm wondering why the following code compiles. #include <iostream> template<class T> void print(T t) { std::cout << t; } namespace ns { struct A {}; } std::ostream& operator<<(std::ostream& out, ns::A) { return out << "hi!"; } int main() { print(ns::A{}); } I was under impression that at the instantiation point unqualified dependent names are looked-up via ADL only - which should not consider the global namespace. Am I wrong? 回答1: This is an interesting case. The workings of name lookup as

C++ compile time counters, revisited

房东的猫 提交于 2020-02-12 08:05:44
问题 TL;DR Before you attempt to read this whole post, know that: a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct; I've packaged the solution into a fameta::counter class that solves a few remaining quirks. You can find it on github; you can see it at work on godbolt. How it all started Since Filip Roséen discovered/invented, in 2015, the black magic that compile time counters are in C++, I have been mildly obsessed with the device

What is the right way to define a friend function outside a template class?

这一生的挚爱 提交于 2020-02-05 13:11:12
问题 If I have a normal class I can "inject" a non-free friend function inside the class. (That among other things can be only be found by ADL). case 1: class A{ double p_; friend double f(A const& a){return a.p_;} }; If instead this is a template class I can do: case 2: template<class T> class A{ double p_; friend double f(A const& a){return a.p_;} // apparently A const& is a synomyn for A<T> const& }; Now suppose that I need to implement f in terms of a class that needs to be defined later. I

The rationale for some of the ADL algorithm steps (C++)

情到浓时终转凉″ 提交于 2020-01-14 04:08:31
问题 In short, I am trying to understand the behavior of Argument-Dependent Lookup in C++. Some statements in ISO/IEC 14882:2017 (E) regarding ADL are not clear to me. I hope somebody would clarify them to me. According to standard, ... Furthermore, if T is a class template specialization, its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provided for template type parameters (excluding template template parameters);

How does “using std::swap” enable ADL?

ぐ巨炮叔叔 提交于 2020-01-10 18:11:07
问题 In What is the copy-and-swap idiom this example is shown: friend void swap(dumb_array& first, dumb_array& second) // nothrow { // enable ADL (not necessary in our case, but good practice) using std::swap; // by swapping the members of two classes, // the two classes are effectively swapped swap(first.mSize, second.mSize); swap(first.mArray, second.mArray); } How exactly does using std::swap enable ADL? ADL only requires an unqualified name. The only benefits I see for using std::swap is that

Why does 'std::endl' require the namespace qualification when used in the statement 'std::cout << std::endl;", given argument-dependent lookup?

狂风中的少年 提交于 2020-01-03 09:42:33
问题 I was looking at the Wikipedia entry on argument-dependent lookup, and (on Jan 04, 2014) the following example was given: #include<iostream> int main() { std::cout << "Hello World, where did operator<<() come from?" << std::endl; } ... with the following comment: Note that std::endl is a function but it needs full qualification, since it is used as an argument to operator<< (std::endl is a function pointer, not a function call). My thought is that the comment is incorrect (or simply unclear).

Why does 'std::endl' require the namespace qualification when used in the statement 'std::cout << std::endl;", given argument-dependent lookup?

岁酱吖の 提交于 2020-01-03 09:41:04
问题 I was looking at the Wikipedia entry on argument-dependent lookup, and (on Jan 04, 2014) the following example was given: #include<iostream> int main() { std::cout << "Hello World, where did operator<<() come from?" << std::endl; } ... with the following comment: Note that std::endl is a function but it needs full qualification, since it is used as an argument to operator<< (std::endl is a function pointer, not a function call). My thought is that the comment is incorrect (or simply unclear).