overload-resolution

How does overload resolution work in the context of private modifier?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 05:12:25
I am unable to understand the output of the following C++ snippet. Should not objc.fn() call A 's fn() since B's fn() is private and should not be visible in C . However, the answer is: the call to fn() is ambiguous. How? #include<iostream> using namespace std; class A{ public: void fn() { cout << "1"; } }; class B{ void fn() { cout << "2"; } }; class C: public A, public B {}; int main(){ C objc; objc.fn(); return 0; } songyuanyao According to the book C++ Templates: The Complete Guide Appendix B.1, At a very high level, a call to a named function can be processed in the following way: The

operator T() not used in assignment

≡放荡痞女 提交于 2019-12-07 05:06:17
问题 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

Overload resolution on operator == with variant generic delegate types

ぃ、小莉子 提交于 2019-12-07 04:56:29
问题 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

Overloaded virtual function call resolution

时光毁灭记忆、已成空白 提交于 2019-12-07 01:35:42
问题 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

SFINAE away a copy constructor

蹲街弑〆低调 提交于 2019-12-06 19:22:57
问题 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? 回答1: This solution uses a base class that is conditionally not

Odd behavior with operator>= overloading

牧云@^-^@ 提交于 2019-12-06 13:39: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 ambiguous use of operator>= and the alternatives are: Mine. The built-in operator>=(long double, int) .

function overload matching template template

99封情书 提交于 2019-12-06 09:35:19
问题 I would expect the last two lines of the first code example to print the same. The types are deducted as I expect and the the overload resolution is also as I expect. However, if I explicitly type qualify the function call, then I get a different result then when the type is deduced. The second code example repeats the exercise replacing overload resolution with specialization. In that case everything works as anyone would expect. Any explanation? EDIT: I added one more line showing what

Partial ordering of function templates containing template parameter packs

烈酒焚心 提交于 2019-12-06 09:32:15
问题 Partial ordering of function templates containing template parameter packs is independent of the number of deduced arguments for those template parameter packs. template<class...> struct Tuple { }; template< class... Types> void g(Tuple<Types ...>); // #1 template<class T1, class... Types> void g(Tuple<T1, Types ...>); // #2 template<class T1, class... Types> void g(Tuple<T1, Types& ...>); // #3 g(Tuple<>()); // calls #1 g(Tuple<int, float>()); // calls #2 g(Tuple<int, float&>()); // calls #3

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

China☆狼群 提交于 2019-12-06 07:35:02
#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? §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 sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F .

Mockito: Verifying overloaded methods with type-compatible arguments

偶尔善良 提交于 2019-12-06 04:23:44
问题 Consider you want to mock an interface using Mockito containing the following method signatures: public void doThis(Object o); public void doThis(Object... o) I need to verify that doThis(Object o) (and not the other method) has been invoked exactly one time. First I thought that the following line would do the trick: verify(mock, times(1)).doThis(anyObject()); However, as this seems to work on Windows, it doesn't on Linux because in this environment, a call to of the other doThis method is