overload-resolution

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

ぃ、小莉子 提交于 2020-01-02 03:59:06
问题 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

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

拥有回忆 提交于 2020-01-02 03:59:04
问题 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

List-initialization and failed overload resolution of initializer_list constructor

你。 提交于 2019-12-31 21:45:19
问题 The below fails to compile with clang35 -std=c++11 : #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } with error init.cpp:15:14: error: type 'double' cannot be narrowed to 'int' in initializer list [-Wc++11

How can I prevent implicit conversions in a function template?

£可爱£侵袭症+ 提交于 2019-12-31 01:25:29
问题 How can I define a function template to prevent implicit conversions? It seems I can prevent implicit conversions using non-template functions but not using function templates. Defining a forwarding reference function template as = delete is too aggressive as it prevents invocation with non-const lvalue references. Defining an function template with a const rvalue argument as =delete [1] does not prevent implicit conversions. Defining an rvalue overload for a specific type as =delete works

Vector initialization with double curly braces: std::string vs int

守給你的承諾、 提交于 2019-12-30 09:54:16
问题 In an answer to this question: Initializing vector<string> with double curly braces it is shown that vector<string> v = {{"a", "b"}}; will call the std::vector constructor with an initializer_list with one element . So the first (and only) element in the vector will be constructed from {"a", "b"} . This leads to undefined behavior, but that is beyond the point here. What I have found is that std::vector<int> v = {{2, 3}}; Will call std::vector constructor with an initializer_list of two

Deducing template arguments during partial ordering when parameters are function parameter pack

血红的双手。 提交于 2019-12-30 05:09:06
问题 N4527 14.8.2.4 [temp.deduct.partial] 3 The types used to determine the ordering depend on the context in which the partial ordering is done: (3.1) — In the context of a function call, the types used are those function parameter types for which the function call has arguments. (3.2) — In the context of a call to a conversion function, the return types of the conversion function templates are used. (3.3) — In other contexts (14.5.6.2) the function template’s function type is used. 4 Each type

Overloaded method-group argument confuses overload resolution?

荒凉一梦 提交于 2019-12-25 08:48:14
问题 The following call to the overloaded Enumerable.Select method: var itemOnlyOneTuples = "test".Select<char, Tuple<char>>(Tuple.Create); fails with an ambiguity error (namespaces removed for clarity): The call is ambiguous between the following methods or properties: 'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>,Func<char,Tuple<char>>)' and 'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>, Func<char,int,Tuple<char>>)' I can certainly understand why not specifying the type

What is the cause of this overload resolution headache?

淺唱寂寞╮ 提交于 2019-12-25 05:38:35
问题 I've got a program where I've got a lot of nested if/switch statements which were repeated in several places. I tried to extract that out and put the switches in a template method class, and then allow clients to overload which switch branches they wanted to specifically handle using overloading: class TraitsA {}; class TraitsB : public TraitsA {}; class Foo { bool traitsB; public: // Whether or not a Foo has traitsB is determined at runtime. It is a // function of the input to the program

Why is this initializer_list constructor a viable overload?

一曲冷凌霜 提交于 2019-12-24 05:12:31
问题 #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } (This question is a follow-up to this.) The above program fails to compile with clang35 -std=c++11 init.cpp:15:14: error: type 'double' cannot be narrowed to

Why is this initializer_list constructor a viable overload?

笑着哭i 提交于 2019-12-24 05:12:06
问题 #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } (This question is a follow-up to this.) The above program fails to compile with clang35 -std=c++11 init.cpp:15:14: error: type 'double' cannot be narrowed to