overload-resolution

Odd behavior with operator>= overloading

爱⌒轻易说出口 提交于 2019-12-08 02:48:09
问题 I'm having a strange behavior with an operator overloading in C++. I have a class, and I need to check if its contents are greater or equal to a long double. I overloaded the >= operator to make this check, my declaration is as follows: bool MyClass::operator>=(long double value) const; I have to say that I also have a cast-to-long-double operator for my class, that works without exceptions only under certain conditions. Now, when I use this operator, the compiler complains that there's an

Why is template overload a better match than a simple conversion?

百般思念 提交于 2019-12-08 00:35:21
问题 #include <iostream> using namespace std; template<typename T> void func(T t) { std::cout << "matched template\n"; } void func(long x) { std::cout << "matched long\n"; } int main() { func(0); } output: matched template In other cases, the non-template function is preferred when overload resolution might be ambiguous, why is this one different? 回答1: §13.3.3 [over.match.best]/p1-2: 1 Define ICSi(F) as follows: (1.1) [inapplicable bullet omitted] (1.2) let ICSi(F) denote the implicit conversion

Swift 3 closure overload resolution

流过昼夜 提交于 2019-12-07 15:55:44
问题 I'm confused by function overload resolution with closures in Swift 3. For example, in the code: func f<T>(_ a: T) { print("Wide") } func f(_ a: (Int)->(Int)) { print("Narrow") } f({(a: Int) -> Int in return a + 1}) I expect Narrow , not Wide , to be printed to the console. Can anyone explain why the more specific overload gets chosen for non-closure arguments but not for closures or is this a compiler bug? Swift 2 exhibited the expected behavior. 回答1: This is probably due to the change in

problems with overload resolution and operator<< for templated types - part 2

与世无争的帅哥 提交于 2019-12-07 15:18:54
问题 Given the following code: #include <string> #include <type_traits> #include <sstream> #include <vector> #include <iostream> using namespace std; namespace has_insertion_operator_impl { typedef char no; typedef char yes[2]; struct any_t { template <typename T> any_t(T const&); }; no operator<<(ostream const&, any_t const&); yes& test(ostream&); no test(no); template <typename T> struct has_insertion_operator { static ostream& s; static T const& t; static bool const value = sizeof(test(s << t))

Type deduction for non-viable function templates

与世无争的帅哥 提交于 2019-12-07 14:18:12
问题 In his answer to this question and the comment section, Johannes Schaub says there's a "match error" when trying to do template type deduction for a function template that requires more arguments than have been passed: template<class T> void foo(T, int); foo(42); // the template specialization foo<int>(int, int) is not viable In the context of the other question, what's relevant is whether or not type deduction for the function template succeeds (and substitution takes place): template<class

When does overload resolution of non-dependent name take place, in definition context or point of instantiation?

隐身守侯 提交于 2019-12-07 09:04:31
问题 3.4 [basic.lookup]/p1 Overload resolution (13.3) takes place after name lookup has succeeded. void g(long); void g(int, int); template<class T> void f() { g(0); } void g(int, int = 0) {} int main(){ f<int>(); } gcc compiles succeed, clang faild. When does overload resolution of non-dependent name take place, in definition context or point of instantiation? Or both are right? 回答1: In both context. [temp.res] 14.6\8 If a hypothetical instantiation of a template immediately following its

Overloading, generic type inference and the 'params' keyword

假装没事ソ 提交于 2019-12-07 06:20:18
问题 I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething<T>(IEnumerable<T> items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(IEnumerable<T> items)"); } Now, I know that this method will often be called with a small number of explicit arguments, so for convenience I add this overload : public static void DoSomething<T>(params T[] items) { // Whatever // For debugging Console.WriteLine("DoSomething

Generic Method Resolution

ε祈祈猫儿з 提交于 2019-12-07 06:10:47
问题 Consider the following code: public class Tests { public void Test() { Assert.AreEqual("Int", DoSomething(1)); } public static string DoSomething<T>(T value) { return "Generic"; } public static string DoSomething(int value) { return "Int"; } } As expected, the non-generic DoSomething method will be invoked. Now consider the following modification: public class Tests { public void Test() { Assert.AreEqual("Int", DoSomething(1)); } public static string DoSomething<T>(T value) { return "Generic"

Overloading of C++ templated functions

时光怂恿深爱的人放手 提交于 2019-12-07 05:30:26
问题 I would think that the following code should be working, but both g++ and clang++ return the exact same error (although Visual C++ 2012 doesn't). #include <iostream> #include <tuple> template <int N, typename T> struct A { }; template <typename Tuple> double result(const Tuple& t, const A<0, typename std::tuple_element<0, Tuple>::type>& a) { return 0; } template <typename Tuple> double result(const Tuple& t, const A<std::tuple_size<Tuple>::value-1, typename std::tuple_element<std::tuple_size

Why does the compiler when using an overload in another assembly sometimes require you to also reference a subassembly?

情到浓时终转凉″ 提交于 2019-12-07 05:17:32
问题 There are quite a few questions/answers about the compiler error mentionend below and how to resolve it, but the question here is asking about some insights why in this case this is required. Why does a project A which uses an overload of a method of another referenced project B, which uses an object of project C in one of it's overloaded signatures require, that you reference project C from project A, even if you never use the object from project C? I guess it must have to do with the