overload-resolution

Why do primitive and user-defined types act differently when returned as 'const' from a function?

巧了我就是萌 提交于 2019-12-02 20:03:54
#include <iostream> using namespace std; template<typename T> void f(T&&) { cout << "f(T&&)" << endl; } template<typename T> void f(const T&&) { cout << "f(const T&&)" << endl; } struct A {}; const A g1() { return {}; } const int g2() { return {}; } int main() { f(g1()); // outputs "f(const T&&)" as expected. f(g2()); // outputs "f(T&&)" not as expected. } The issue description is embedded in the code. My compiler is clang 5.0 . I just wonder: Why does C++ treat built-in types and custom types differently in such a case? I don't have a quote from the standard, but cppreference confirms my

How is ambiguity determined in the overload resolution algorithm?

爷,独闯天下 提交于 2019-12-02 15:21:56
I'm trying to understand the overloading resolution method. Why is this ambiguous: void func(double, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); } but this isn't? void func(int, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); } In the first case there are 2 exact parameters matches and 2 conversions against 1 exact match and 3 conversions, and in the second case there are 3 exact matches and 1 conversion against 1 exact matches and 3 conversions. So why is one ambiguous and one is not? What is the

Why can I prevent implicit conversions for primitives but not user-defined types?

孤人 提交于 2019-12-02 12:03:44
问题 The High Integrity C++ Standards suggest that rvalue arguments to functions can be deleted thus preventing implicit conversions. http://www.codingstandard.com/rule/8-3-4-define-delete-functions-with-parameters-of-type-rvalue-reference-to-const/ I've found that the behaviour for primitives and user-defined types is very different. struct A { }; struct B { B(const A& ) {} }; template <class T> void foo(const T&&) = delete; // 1 - deleted rvalue overload. const intentional. void foo(B) {} // 2

Forcing late method resolution in case of class inheritance in c++

怎甘沉沦 提交于 2019-12-02 09:26:44
Consider the following class structure:- class foo { public: int fun () { cout << "in foo" << endl; } }; class bar_class1:public foo { public: int fun () { cout << "in bar_class1" << endl; } }; class bar_class2:public foo { public: float fun () { cout << "in bar_class2" << endl; } }; main () { foo * foo_pointer = new bar_class1(); foo_pointer->fun(); } The output of the above program is in foo . Is there a way, that using a pointer of type foo * which actually points to an object of type bar_class1 or bar_class2 , we can call the fun function of the derived class instead of the base class? I

Why can I prevent implicit conversions for primitives but not user-defined types?

南笙酒味 提交于 2019-12-02 07:15:06
The High Integrity C++ Standards suggest that rvalue arguments to functions can be deleted thus preventing implicit conversions. http://www.codingstandard.com/rule/8-3-4-define-delete-functions-with-parameters-of-type-rvalue-reference-to-const/ I've found that the behaviour for primitives and user-defined types is very different. struct A { }; struct B { B(const A& ) {} }; template <class T> void foo(const T&&) = delete; // 1 - deleted rvalue overload. const intentional. void foo(B) {} // 2 void foo(int) {} // 3 int main(int argc, char* argv[]) { A a; foo(a); // This resolves to 2 foo(3.3); //

Building libspline for Matlab on Windows - ambiguous call to overloaded function 'pow'

南笙酒味 提交于 2019-12-02 04:27:12
问题 I'm trying to build libspline for Matlab on Windows, available here: http://ttic.uchicago.edu/~smaji/projects/libspline-release1.0.tar.gz I get the following error: >> make additiveModel.cpp additiveModel.cpp(156) : error C2668: 'pow' : ambiguous call to overloaded function C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\math.h(583): could be 'long double pow(long double,int)' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\math.h(535): or 'float pow(float,int)' C:\Program

Building libspline for Matlab on Windows - ambiguous call to overloaded function 'pow'

烈酒焚心 提交于 2019-12-02 02:02:05
I'm trying to build libspline for Matlab on Windows, available here: http://ttic.uchicago.edu/~smaji/projects/libspline-release1.0.tar.gz I get the following error: >> make additiveModel.cpp additiveModel.cpp(156) : error C2668: 'pow' : ambiguous call to overloaded function C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\math.h(583): could be 'long double pow(long double,int)' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\math.h(535): or 'float pow(float,int)' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\math.h(497): or 'double pow(double,int)' while trying to

Function with parameter type that has a copy-constructor with non-const ref chosen?

妖精的绣舞 提交于 2019-12-02 00:43:05
Some time ago I was confused by the following behavior of some code when I wanted to write a is_callable<F, Args...> trait. Overload resolution won't call functions accepting arguments by non-const ref, right? Why doesn't it reject in the following because the constructor wants a Test& ? I expected it to take f(int) ! struct Test { Test() { } // I want Test not be copyable from rvalues! Test(Test&) { } // But it's convertible to int operator int() { return 0; } }; void f(int) { } void f(Test) { } struct WorksFine { }; struct Slurper { Slurper(WorksFine&) { } }; struct Eater { Eater(WorksFine)

Overload Resolution/Ambiguity in name lookup(which one)

冷暖自知 提交于 2019-12-01 19:50:55
$7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(int); }; struct D : B, C { using C::x; int x(double); }; int f(D* d) { return d->x(); // ambiguous: B::x or C::x } The comment in the code in 'f' indicates that one can expect ambiguity between 'B::x' or 'C::x'. However, on compiling with g++(ideone) or Comeau the errors are slightly different. These errors instead of indicating ambiguity in B::x or C::x indicate the fact that A is an ambiguous base of D prog.cpp: In function ‘int f(D*)’: prog.cpp:16: error: ‘A’ is an ambiguous base of ‘D’ And

Ambiguous method call with Action<T> parameter overload

我怕爱的太早我们不能终老 提交于 2019-12-01 17:27:12
I encountered some unexpected compiler behaviour when calling overloaded method with different Action<T> variations. Let's say I have this class Test and I'm creating its instance in the CallTest constructor. public class Test { public Test(Action<long> arg) { } public Test(Action<decimal> arg) { } } public class CallTest { public CallTest() { Test t = new Test(TestDecimal); } public void TestDecimal(decimal arg) { } public void TestLong(long arg) { } } When calling the Test constructor with either TestDecimal or TestLong as a parameter I'm receiving the following error: The call is ambiguous