overload-resolution

C++11: Universal executor

亡梦爱人 提交于 2019-12-05 10:24:59
I would to know how get this code compiles: // test3.cpp #include <iostream> using namespace std; template<typename R, typename... rArgs> R universal_exer(R(*f)(rArgs...), rArgs... args) { return (*f)(forward<rArgs>(args)...); } int addition(int a) { return a; } int addition(int a, int b) { return a + b; } template<typename... Args> int addition(int a, int b, Args... args) { return a + b + addition(args...); } int main() { cout << universal_exer(&addition, 1) << endl; } Error message (gcc 4.7.2): test3.cpp: In function 'int main()': test3.cpp:31:40: error: no matching function for call to

operator T() not used in assignment

限于喜欢 提交于 2019-12-05 10:14:31
I am a bit confused by this. Lets assume I have a helper class Data class Data { public: Data(const QVariant &value) : m_Variant(value) { } operator QString() const { return m_Variant.toString(); } private: QVariant m_Variant; }; then when I do this: Data d("text"); QString str = d; //str becomes "text" it works but when I continue to do: Data d2("text2"); str = d2; //does not compile it fails complaining: ambiguous overload for 'operator=' (operand types are 'QString' and 'Data') candidates are: ... QString &operator=(const QString &); QString &operator=(QString &&); QString &operator=(const

Overload resolution and partial template ordering

本小妞迷上赌 提交于 2019-12-05 10:09:07
问题 Consider this simple pair of function templates. template <typename T> void foo(T& ) { std::cout << __PRETTY_FUNCTION__ << '\n'; } template <typename C> void foo(const C& ) { std::cout << __PRETTY_FUNCTION__ << '\n'; } If we call foo with a non-const argument: int i = 4; foo(i); The T& overload is preferred based on [over.ics.rank]/3.2.6, since the deduced reference int& is less cv -qualified than the deduced reference const int& . However, if we call foo with a const argument: const int ci =

Overload resolution on operator == with variant generic delegate types

房东的猫 提交于 2019-12-05 09:53:09
What are the precise rules for overload resolution with == between two expressions of delegate type? Consider the following code (where using System; is needed): static class ProgramA { static void TargetMethod(object obj) { } static void Main() { Action<object> instance1 = TargetMethod; Action<object> instance2 = TargetMethod; Action<string> a1 = instance1; Action<Uri> a2 = instance2; Console.WriteLine((object)a1 == (object)a2); Console.WriteLine((Delegate)a1 == (Delegate)a2); Console.WriteLine((Action<object>)a1 == (Action<object>)a2); Console.WriteLine(a1 == a2); // warning CS0253: Possible

Overloaded virtual function call resolution

落爺英雄遲暮 提交于 2019-12-05 05:16:08
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc class Bbase{ public: virtual void f(Abase* a); virtual void f(A1* a); virtual void f(A2* a); }; class B1:public Bbase{ public: void f(A1* a); }; class B2:public Bbase{ public: void f(A2* a); }; int main(){ A1* a1=new A1(); A2* a2=new A2(); Bbase* b1=new B1(); Bbase* b2=new B2(); b1->f(a1); // calls B1::f(A1*), ok b2->f(a2); // calls B2::f(A2*), ok b2->f(a1); // calls Bbase::f(A1*), ok b1->f(a2); // calls Bbase::f(A2*), no- want B1::f(A1*)! } I'm interested to know why C++ chooses to resolve

mypy error, overload with Union/Optional, “Overloaded function signatures 1 and 2 overlap with incompatible return types”

喜夏-厌秋 提交于 2019-12-05 04:57:19
So, let's start with an example. Suppose we have several types that can be combined together, let's say we are using __add__ to implement this. Unfortunately, due to circumstances beyond our control, everything has to be "nullable", so we are forced to use Optional everywhere. from typing import Optional, List, overload class Foo: value: int def __init__(self, value: int) -> None: self.value = value def __add__(self, other: 'Foo') -> 'Optional[Foo]': result = self.value - other.value if result > 42: return None else: return Foo(result) class Bar: value: str def __init__(self, value: str) ->

Implicit conversion operator priority

随声附和 提交于 2019-12-05 01:07:43
In the following piece of code ( live on coliru ): #include <iostream> #include <string> int main() { struct S { operator bool () const { return false; } operator std::string () const { return "false"; } } s; std::cout << s << "\n"; // outputs 0 } How does the compiler choose to pick the implicit conversion to bool over std::string ? My hypothesis is that in this case, it might be purely the order of declaration of the different flavours of std::basic_ostream::operator<< , but is it all? Does the standard say something about picking a specific implicit conversion? Recall that std::string is

SFINAE away a copy constructor

旧街凉风 提交于 2019-12-05 01:03:26
Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I pass as class template parameters. The problem is, that SFINAE only works on templates and a copy constructor/assignment operator can't be a template. Does there exist a workaround? stefan This solution uses a base class that is conditionally not copyable (by explicitely marking the copy constructor and copy assignment operator as deleted). template

Why does C# compiler overload resolution algorithm treat static and instance members with equal signature as equal?

十年热恋 提交于 2019-12-04 23:26:28
Let we have two members equal by signature, but one is static and another - is not: class Foo { public void Test() { Console.WriteLine("instance"); } public static void Test() { Console.WriteLine("static"); } } but such code generate brings a compiler error: Type 'Foo' already defines a member called 'Test' with the same parameter types But why? Let we compiled that successfully, then: Foo.Test() should output "static" new Foo().Test(); should output "instance" Can't call the static member instead of instance one because in this case another, more reasonable compiler error will occur: Member

How does method overload resolution work (LINQ Where extension method)?

ⅰ亾dé卋堺 提交于 2019-12-04 22:56:56
问题 If I have a variable of type IQueryable<T> I have four extension methods for Where in namespace Systm.Linq available: public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate); public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, int, bool>> predicate); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, int,