overloading

C++ overloaded function as template argument

好久不见. 提交于 2019-12-17 12:53:48
问题 the simplified version of my code is here int foo(int x) { return x; } int foo(int x, int y) { return x+y; } template<typename unary_func> int bar(int k, unary_func f) { return f(k); } int main() { bar(3, foo); return 0; } Is there a way to tell the compiler what I want to pass as argument is the first `foo'? 回答1: You can give an explicit template argument: bar<int(int)>(3, foo); or cast the ambiguous function name to a type from which the template argument can be deduced: bar(3, static_cast

C++ overloaded function as template argument

假装没事ソ 提交于 2019-12-17 12:53:07
问题 the simplified version of my code is here int foo(int x) { return x; } int foo(int x, int y) { return x+y; } template<typename unary_func> int bar(int k, unary_func f) { return f(k); } int main() { bar(3, foo); return 0; } Is there a way to tell the compiler what I want to pass as argument is the first `foo'? 回答1: You can give an explicit template argument: bar<int(int)>(3, foo); or cast the ambiguous function name to a type from which the template argument can be deduced: bar(3, static_cast

Why is it not possible to overload class templates?

馋奶兔 提交于 2019-12-17 10:56:37
问题 Reading this question made me wonder: is there a technical reason for disallowing class templates overloads? By overloading, I mean having several templates with the same names, but different parameters, for instance template <typename T> struct Foo {}; template <typename T1, typename T2> struct Foo {}; template <unsigned int N> struct Foo {}; The compiler manages to handle overloaded functions and function templates, wouldn't it be possible to apply the same techniques (e.g. name mangling)

Shadows vs Overloads in VB.NET

≯℡__Kan透↙ 提交于 2019-12-17 10:40:11
问题 When we have new in C#, that personally I see only as a workaround to override a property that does not have a virtual/overridable declaration, in VB.NET we have two "concepts" Shadows and Overloads . In which case prefer one to another? 回答1: There are three closely related concepts; overriding, shadowing and overloading. Overriding is when you make a new implementation for a virtual method. Shadowing is when you make a new non-virtual implementation for a method. Overloading is when you add

Overload resolution with ref-qualifiers

流过昼夜 提交于 2019-12-17 09:40:37
问题 While working with ref-qualified function overloads, I'm getting different results from GCC (4.8.1) and Clang (2.9 and trunk) . Consider the following code: #include <iostream> #include <utility> struct foo { int& bar() & { std::cout << "non-const lvalue" << std::endl; return _bar; } //~ int&& bar() && //~ { //~ std::cout << "non-const rvalue" << std::endl; //~ return std::move(_bar); //~ } int const& bar() const & { std::cout << "const lvalue" << std::endl; return _bar; } int const&& bar()

Why does the Scala compiler disallow overloaded methods with default arguments?

南楼画角 提交于 2019-12-17 07:07:11
问题 While there might be valid cases where such method overloadings could become ambiguous, why does the compiler disallow code which is neither ambiguous at compile time nor at run time? Example: // This fails: def foo(a: String)(b: Int = 42) = a + b def foo(a: Int) (b: Int = 42) = a + b // This fails, too. Even if there is no position in the argument list, // where the types are the same. def foo(a: Int) (b: Int = 42) = a + b def foo(a: String)(b: String = "Foo") = a + b // This is OK: def foo

Java overloading and overriding

感情迁移 提交于 2019-12-17 06:46:16
问题 We always say that method overloading is static polymorphism and overriding is runtime polymorphism. What exactly do we mean by static here? Is the call to a method resolved on compiling the code? So whats the difference between normal method call and calling a final method? Which one is linked at compile time? 回答1: Method overloading means making multiple versions of a function based on the inputs. For example: public Double doSomething(Double x) { ... } public Object doSomething(Object y) {

C++ overload operator [ ][ ]

我是研究僧i 提交于 2019-12-17 06:45:31
问题 I have class CMatrix, where is "double pointer" to array of values. class CMatrix { public: int rows, cols; int **arr; }; I simply need to access the values of matrix by typing: CMatrix x; x[0][0] = 23; I know how to do that using: x(0,0) = 23; But I really need to do that the other way. Can anyone help me with that? Please? Thank you guys for help at the end I did it this way... class CMatrix { public: int rows, cols; int **arr; public: int const* operator[]( int const y ) const { return

Method overload resolution in java

非 Y 不嫁゛ 提交于 2019-12-17 06:42:52
问题 Here is what I know about overload resolution in java: The process of compiler trying to resolve the method call from given overloaded method definitions is called overload resolution. If the compiler can not find the exact match it looks for the closest match by using upcasts only (downcasts are never done). Here is a class: public class MyTest { public static void main(String[] args) { MyTest test = new MyTest(); Integer i = 9; test.TestOverLoad(i); } void TestOverLoad(int a){ System.out

Scala double definition (2 methods have the same type erasure)

泄露秘密 提交于 2019-12-17 06:25:16
问题 I wrote this in scala and it won't compile: class TestDoubleDef{ def foo(p:List[String]) = {} def foo(p:List[Int]) = {} } the compiler notify: [error] double definition: [error] method foo:(List[String])Unit and [error] method foo:(List[Int])Unit at line 120 [error] have same type after erasure: (List)Unit I know JVM has no native support for generics so I understand this error. I could write wrappers for List[String] and List[Int] but I'm lazy :) I'm doubtful but, is there another way