overload-resolution

Overload Resolution in a Namespace

天大地大妈咪最大 提交于 2019-12-23 11:48:29
问题 I am attempting to call an overloaded function inside a namespace and am struggling a bit. Working Example 1: No namespace class C {}; inline void overloaded(int) {} template<typename T> void try_it(T value) { overloaded(value); } inline void overloaded(C) {} int main() { try_it(1); C c; try_it(c); return 0; } Working Example 2: All overloads defined before template class C {}; namespace n { inline void overloaded(int) {} inline void overloaded(C) {} } template<typename T> void try_it(T value

How do I write [DefaultValue(null)] in VB.NET? <DefaultValue(Nothing)> does not compile

为君一笑 提交于 2019-12-23 10:17:06
问题 This isn't as trivial as it appears to be. It's a follow-up to this question. Let's say I have a Windows Forms user control with a property: // using System.ComponentModel; [DefaultValue(null)] public object DataSource { … } If I translate this to VB.NET, I would try this: 'Imports System.ComponentModel <DefaultValue(Nothing)> Public Property DataSource As Object … End Property This won't work because the compiler has problems choosing the correct overload of DefaultValueAttribute's

How to dump candidates in function overload resolution?

让人想犯罪 __ 提交于 2019-12-23 10:15:01
问题 How can I dump candidate functions (or viable functions or best viable functions) for a function invocation? I know g++ provides an option to dump class hierarchy. (In fact, Visual Studio 2010 provides a similar option, but it's undocumented. I remember reading something about it—maybe in the VC++ team blog—but I can't remember it clearly.) Recently, I have been reading about overload resolution in the C++0x draft, and it really embarrassed me. Does any compiler provide an option to dump

function call ambiguity with pointer, reference and constant reference parameter

懵懂的女人 提交于 2019-12-22 10:45:49
问题 What I am trying to do is, allow a pointer, reference or constant reference to be passed with the setter function: class A{ std::string * p; std::string st; public: A():p(0) {} A& setS(const std::string& s){ std::cout<<"called with const std::string&\n"; st = s; p = &st; return *this; } A& setS(std::string& s) { std::cout<<"called with std::string&\n"; p = &s; return *this; } A& setS(std::string* s) { std::cout<<"called with std::string*\n"; p = s; return *this; } }; int main(){ std::string s

Get best matching overload from set of overloads

淺唱寂寞╮ 提交于 2019-12-22 07:08:09
问题 Let's say I have a class as follows: public class AcceptMethods { public int Accept(string s, int k = 1) { return 1; } public int Accept(object s) { return 2; } public int Accept(IEnumerable<object> s) { return 7; } public int Accept(IList<object> s) { return 4; } } Now, if I try to consume this in code, I use something like this: object[] list = new object[] { "a", new object[0], "c", "d" }; Assert.AreEqual(7, list.Select((a)=>((int)new AcceptMethods().Accept((dynamic)a))).Sum()); The reason

Last resort/catch-all/fallback template overload

南笙酒味 提交于 2019-12-22 05:11:59
问题 As is evident in a question I asked previously, Overload resolution, templates and inheritance, a template overload will be chosen before an overload which requires derived-to-base conversion. However, is there a way to provide a fallback overload which is only selected as an absolute last resort if there is nothing else that matches? In this particular case enable_if could be used but that would not be extensible, unfortunately. Like this: // My library has this and has no knowledge of the

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

左心房为你撑大大i 提交于 2019-12-22 02:06:06
问题 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

Which to use: move assignment operator vs copy assignment operator

夙愿已清 提交于 2019-12-21 04:30:49
问题 I don't seem to get why would you use the move assignment operator : CLASSA & operator=(CLASSA && other); //move assignment operator over, the copy assignment operator : CLASSA & operator=(CLASSA other); //copy assignment operator The move assignment operator takes an r-value reference only e.g. CLASSA a1, a2, a3; a1 = a2 + a3; In the copy assignment operator , other can be constructor using a copy constructor or a move constructor (if other is initialized with an rvalue, it could be move

Why does the compiler find my function if is not yet declared?

余生颓废 提交于 2019-12-21 03:43:13
问题 Contrary to my expectations, this program works: #include <iostream> namespace a { struct item{}; } namespace b { struct item{}; } template<typename T> void func(T t) { do_func(t); } int main() { func(a::item{}); func(b::item{}); } namespace a { void do_func(item) { std::cout << "a::func\n"; } } namespace b { void do_func(item) { std::cout << "b::func\n"; } } Output: a::func b::func Verifications with online compilers: gcc 4.8 clang 3.4 If the instantation of func<T> occurs in the body of

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

纵然是瞬间 提交于 2019-12-20 06:46:14
问题 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