overloading

How is <tgmath.h> implemented?

爷,独闯天下 提交于 2019-12-09 05:59:40
问题 C doesn't have (to the best of my knowledge) overloading or templates, right? So how can a set of type-agnostic functions with the same name exist in plain ol' C? The usual compile-time trickery would involve a whole bunch of macros, wouldn't it? 回答1: There's a great explanation of how it works in GCC here. Also, if anyone can solve the medium-difficulty exercise, I'd love to know the answer. 来源: https://stackoverflow.com/questions/2726712/how-is-tgmath-h-implemented

Why does this function pointer assignment work when assigned directly but not with the conditional operator?

允我心安 提交于 2019-12-09 05:01:31
问题 (No #include's were used for this example, compiled on MacOS10.14, Eclipse IDE, with g++, options -O0 -g3 -Wall -c -fmessage-length=0) Assuming this variable declaration: int (*fun)(int); This fails to compile with "invalid overload of std::toupper and std::tolower". fun = (1 ? std::toupper : std::tolower); // ERROR, invalid overload And this compiles OK: if (1) { fun = std::toupper; // OK } else { fun = std::tolower; // OK } 回答1: std::toupper (1 and 2) and std::tolower (1 and 2) are

Can bin() be overloaded like oct() and hex() in Python 2.6?

 ̄綄美尐妖づ 提交于 2019-12-09 04:39:21
问题 In Python 2.6 (and earlier) the hex() and oct() built-in functions can be overloaded in a class by defining __hex__ and __oct__ special functions. However there is not a __bin__ special function for overloading the behaviour of Python 2.6's new bin() built-in function. I want to know if there is any way of flexibly overloading bin() , and if not I was wondering why the inconsistent interface? I do know that the __index__ special function can be used, but this isn't flexible as it can only

C++11 Lambda functions implicit conversion to bool vs. std::function

自作多情 提交于 2019-12-09 03:02:45
问题 Consider this simple example code: #include <functional> #include <iostream> void f(bool _switch) { std::cout << "Nothing really" << std::endl; } void f(std::function<double (int)> _f) { std::cout << "Nothing really, too" << std::endl; } int main ( int argc, char* argv[] ) { f([](int _idx){ return 7.9;}); return 0; } It fails to compile: $ g++ --std=c++11 main.cpp main.cpp: In function ‘int main(int, char**)’: main.cpp:15:33: error: call of overloaded ‘f(main(int, char**)::<lambda(int)>)’ is

Why does C# allow ambiguous function calls through optional arguments?

帅比萌擦擦* 提交于 2019-12-09 02:32:54
问题 I came across this today, and I am surprised that I haven't noticed it before. Given a simple C# program similar to the following: public class Program { public static void Main(string[] args) { Method(); // Called the method with no arguments. Method("a string"); // Called the method with a string. Console.ReadLine(); } public static void Method() { Console.WriteLine("Called the method with no arguments."); } public static void Method(string aString = "a string") { Console.WriteLine("Called

Method overloading with variable arguments (varargs) [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-09 02:21:46
问题 This question already has answers here : Varargs Java Ambiguous Call (2 answers) Closed 4 years ago . I am surprised by seeing the output of this code : public class File { public static void main(String[] args) { movie(); } static void movie(double... x) { System.out.println("No varargs"); } static void movie(int... x) { System.out.println("One argument"); } } It outputs, One argument Why is it so ? I thought that this code would not compile because the call to movie() is ambiguous , but it

Inheriting from instance in Python

随声附和 提交于 2019-12-08 23:45:07
问题 In Python, I would like to construct an instance of the Child's class directly from an instance of the Parent class. For example: A = Parent(x, y, z) B = Child(A) This is a hack that I thought might work: class Parent(object): def __init__(self, x, y, z): print "INITILIZING PARENT" self.x = x self.y = y self.z = z class Child(Parent): def __new__(cls, *args, **kwds): print "NEW'ING CHILD" if len(args) == 1 and str(type(args[0])) == "<class '__main__.Parent'>": new_args = [] new_args.extend(

Typeclasses and overloading, what is the connection?

隐身守侯 提交于 2019-12-08 22:51:00
问题 I am currently trying to wrap my head around typeclasses and instances and I don't quite understand the point of them yet. I have two questions on the matter so far: 1) Why is it necessary to have a type class in a function signature when the function uses some function from that type class. Example: f :: (Eq a) => a -> a -> Bool f a b = a == b Why put (Eq a) in the signature. If == is not defined for a then why not just throw the error when encountering a == b ? What is the point in having

using and overloading a template member function of a base class?

邮差的信 提交于 2019-12-08 22:10:17
问题 In the following, struct Y overloads X 's member function f . Both overloads are template functions, but take different arguments ( typename and int ), to be explicitly specified: struct X { template <typename> static bool f() { return true; } }; struct Y : public X { using X::f; template <int> static bool f() { return false; } }; int main() { std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl; } This prints 1 0 using gcc, as expected. However, clang (3.3) complains that [...] error

C++/CLI: Is overloading on return type only possible?

本秂侑毒 提交于 2019-12-08 20:57:33
If I understand well, in C#, it is possible to do public class X : ICloneable { public X Clone() { ... } object ICloneable.Clone() { return Clone(); } // This calls the above } according to this thread . This kind of overloading is forbidden in C++, since it only depends on the return type. Now, I would like to do this exact thing with ICloneable in C++/CLI. Is there a way ? This type of overloading is allowed in C# not because of different return type, but because of explicit implementation of interface - ICloneable.Clone . About C++/CLI look here: http://msdn.microsoft.com/en-us/library