argument-dependent-lookup

Use of funcName() before deduction of auto — why in one case but not the other?

旧时模样 提交于 2019-12-12 18:33:55
问题 Consider the following code: #include <unordered_map> #include <tuple> namespace Test { template<typename State> struct StateTableEntry { State state; }; template<typename State> using StateRow = std::unordered_map<int,StateTableEntry<State>>; template<typename StateRowValueType> auto& entryBAD(StateRowValueType& row) { return row.second; } template<typename StateRowValueType> auto entryOK(StateRowValueType& row) -> decltype((row.second)) { return row.second; } } template<class T,int I,class

Does ADL work for the global namespace?

╄→гoц情女王★ 提交于 2019-12-12 12:25:26
问题 Examples such as enabling outputting of std types explain how ADL can be used to "inject" a certain function/operator, depending on the type the fn/op is applied to. I was wondering wheter ADL fully applies to the global namespace, that is, whether a type declared (or made available via using ) at global namespace scope makes ADL look for matching functions in the global namespace? Specifically, are these equivalent wrt. ADL?: // 1 - at global namespace scope struct GlobalType {}; template<

Is there a legal way to print tuples and pairs using operator<<?

孤者浪人 提交于 2019-12-12 07:58:38
问题 I have a set of templates/functions that allow me to print a tuple/pair assuming that each type in the tuple/pair has operator<< defined for it. Unfortunately, due to 17.4.3.1, it is illegal to add my operator<< overloads to std . Is there another way to get ADL to find my operator<< ? If not, is there any actual harm in wrapping my overload in namespace std{} ? The code for anyone interested: (I'm using gcc-4.5) namespace tuples { using ::std::tuple; using ::std::make_tuple; using ::std::get

ADL for class in anonymous namespace [closed]

北城以北 提交于 2019-12-11 07:05:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . Does anybody know why the next piece of code isn't compiled on Clang 4.0.1? I have the next error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup There is some file test.cpp #include <vector> #include <iostream> namespace Wrapper { template

How can I use ADL in template deduction?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:06:10
问题 I have a class with an in-class defined friend function that I would ideally not modify (it comes from a header that's already deployed) #include <numeric> #include <vector> namespace our_namespace { template <typename T> struct our_container { friend our_container set_union(our_container const &, our_container const &) { return our_container{}; } }; } // namespace our_namespace set_union is not declared outside the struct or namespace, but can normally be found through argument-dependent

How Can I Avoid Explicitly Specializing Templatized Functions With Argument Dependent Lookup

試著忘記壹切 提交于 2019-12-11 01:47:23
问题 So I've written an answer which uses a templatized function to select object type. I've defined the types: struct pt { double t; double e; double c_vis; double c_invis; }; struct pt_weighted : pt { double sigma; }; And my templatized function looks like: template <typename T> void foo() { for(T point; dataFile >> point;) { set.curve.push_back(point); // store point data_numPoints++; // collect some stats set.curveAvg += point.e; } } Given that minimizator_weighted decides which type to use at

What is the preference of function/method/template name resolving in C++?

隐身守侯 提交于 2019-12-11 00:29:59
问题 How does the C++ compiler decide which function/method to call if there are multiple possibilities? In my specific case I have the standard free function of the C++ Run time and I also have a templated free variant, like this: // The definitions of the C++ Run Time Library (from memory.h) extern malloc(size_t s); extern void free(void *p); // Our own memory management functions extern void *OurMalloc(size_t s); extern void OurFree(void *p); // Own variants to overrule malloc and free (instead

cannot access namespace scope friend explicitly

狂风中的少年 提交于 2019-12-10 22:38:58
问题 I had an issue today where ADL wasn't finding a static member function for a type defined inside a class. That is, in the below example, str(foo::Foo::Enum) isn't located via ADL without explicitly scoping it, foo::Foo::str(foo::Foo::Enum) namespace foo { struct Foo { enum Enum { FOO1, FOO2 }; static const char* str(Enum e); }; } foo::Foo::Enum e = foo::Foo::FOO1; const char* s = str(e); // ADL doesn't work I found this SO question, and as stated in the accepted answer, changing it to a

c++: the context of an unqualified name lookup in a template

↘锁芯ラ 提交于 2019-12-10 20:29:53
问题 I tried to consult the standard on the resolution of do_run, and found "For the part of the lookup using unqualified name lookup (3.4.1) or qualified name lookup (3.4.3), only function declarations from the template definition context are found". What exactly is the context? In the following example, do_run(int) somehow "hides" do_run(domain::mystruct) , and the compiler complains that o can't be converted to int . If I comment out do_run(int) , do_run(domain::mystruct) becomes visible to run

Operator overloading, name resolution and namespaces

流过昼夜 提交于 2019-12-10 19:53:09
问题 I would like some light to be shed on a puzzling situation involving ADL, namespaces and operator overloading. Let Foo be a library which defines a class ( Deriv ) in its own namespace, along with a templated operator * which returns another class. namespace Foo { class Deriv {}; class Another {}; template <typename T> Another operator* ( T x, const Deriv& d ) { return Another();} } Now I use Foo's class in my own library Bar, which defines another operator * , this time only for float .