Category : overload-resolution

Why is the using-declaration inside struct S necessary? struct S inherits from both l1‘s and l2‘s types, which means their respective operator()s are considered when calling s(123). This is corroborated by the fact that, once I remove using, gcc tells me the "request for member ‘operator()’ is ambiguous", and goes on to list both lambdas ..

Read more

Consider the following code: struct A { int& getAttribute(); const int& getAttribute() const; }; std::vector<int> foo(const std::vector<A>& as) { std::vector<int> ints; std::transform(as.begin(), as.end(), std::back_inserter(ints), std::mem_fn(&A::getAttribute)); return ints; } Its compilation (g++ -std=c++14 -c mem_fn.cpp, g++ version 7.5.0) fails with the following error: error: no matching function for call to ‘mem_fn(<unresolved overloaded function type>)’ However, If ..

Read more

I have both template and non-template member function overloaded like the code below: struct TypeI {}; using FuncI = std::function<void(TypeI&)>; struct TypeA { FuncI m_fn; TypeA(FuncI const& p_fn = nullptr){ m_fn = p_fn; } }; struct TypeX { void add_A(int p, FuncI const& p_func) { if (!p_func) return; //…some code add_A(p, TypeA{ p_func }); //…some ..

Read more

The code below gives an error in the a::b::print function: Invalid operands to binary expression (‘std::ostream’ (aka ‘basic_ostream<char>’) and ‘Foo’). The error goes away if I comment out the operator<< overload in namespace b. But I don’t understand why that makes a difference because it has a different signature to the operator<< overload in namspace ..

Read more

At the doc page of boost::hana::always I read that always(x) is a function such that always(x)(y…) == x for any y…. This makes me think that it shouldn’t behave any differently than this lambda: [](auto const&…){ return false; }. However it does. For instance, the following code prints 11, but if I change the third ..

Read more

#include <iostream> #include <string> struct mystruct{ mystruct(std::string s){ std::cout<<__FUNCTION__ <<" String "<<s; } explicit mystruct(bool s) { std::cout<<__FUNCTION__<<" Bool "<<s; } }; int main() { const char* c ="hello"; mystruct obj(c); return 0; } output: mystruct Bool 1 Why const char* implicitly converted to bool rather than std::string, though constructor requires explicit type ? How ..

Read more