overloading

Template function overloading with identical signatures, why does this work?

巧了我就是萌 提交于 2019-12-04 17:45:42
问题 Minimal program: #include <stdio.h> #include <type_traits> template<typename S, typename T> int foo(typename T::type s) { return 1; } template<typename S, typename T> int foo(S s) { return 2; } int main(int argc, char* argv[]) { int x = 3; printf("%d\n", foo<int, std::enable_if<true, int>>(x)); return 0; } output: 1 Why doesn't this give a compile error? When the template code is generated, wouldn't the functions int foo(typename T::type search) and int foo(S& search) have the same signature?

Why is a template with deduced return type not overloadable with other versions of it?

爷,独闯天下 提交于 2019-12-04 17:39:32
问题 Why are the following two templates incompatible and can't be overloaded? #include <vector> template<typename T> auto f(T t) { return t.size(); } template<typename T> auto f(T t) { return t.foobar(); } int main() { f(std::vector<int>()); } I would think they are (more or less) equivalent with the following which compiles fine (as we cannot do decltype auto(t.size()) I can't give an exact equivalent without some noise..). template<typename T> auto f(T t) -> decltype(t.size() /* plus some decay

overloaded __iter__ is bypassed when deriving from dict

随声附和 提交于 2019-12-04 17:34:49
Trying to create a custom case-insensitive dictionary, I came the following inconvenient and (from my point-of-view) unexpected behaviour. If deriving a class from dict , the overloaded __iter__ , keys , values functions are ignored when converting back to dict . I have condensed it to the following test case: import collections class Dict(dict): def __init__(self): super(Dict, self).__init__(x = 1) def __getitem__(self, key): return 2 def values(self): return 3 def __iter__(self): yield 'y' def keys(self): return 'z' if hasattr(collections.MutableMapping, 'items'): items = collections

overloading new and delete

白昼怎懂夜的黑 提交于 2019-12-04 16:49:01
I try to follow this article: http://flipcode.com/archives/How_To_Find_Memory_Leaks.shtml to overload my new and delete functions in order to track memory leaks. however - if I try to compile, I get a C2365: "operator new": redefinition; previous definition was a "function" in the file xdebug xdebug gets included in xlocale - however, i can't find where my project is including xlocale I'm using MFC for multithreading in my project. Can someone tell me how I can get my memory leak tracking to work? //edit: So this is my findMemoryLeak.h which i include in the end of stdafx.h #ifndef

C++ Operator Overloading [ ] for lvalue and rvalue

怎甘沉沦 提交于 2019-12-04 16:34:34
I made a class Array which holds an integer array. From the main function, I'm trying to get an element of the array in Array using [ ] as we do for arrays declared in main. I overloaded the operator [ ] as in the following code; the first function returns an lvalue and the second an rvalue (Constructors and other member functions are not shown.) #include <iostream> using namespace std; class Array { public: int& operator[] (const int index) { return a[index]; } int operator[] (const int index) const { return a[index]; } private: int* a; } However, when I try to call those two functions from

How to overload the PowerShell inbuilt class's methods

江枫思渺然 提交于 2019-12-04 15:07:16
Can I overload the PowerShell inbuilt class's methods? If yes then how? Any code sample would be great. Essentially, I am trying to overload the Equals method of a Hashtable Dictionary PowerShell object to do appropriate comparisons. I know that you can overload a cmdlet or a function in PowerShell and whatever is the latest definition that is taken up. But I am trying to overload not a cmdlet or a function, but trying to overload inbuilt method for existing class. I have already checked this post: Function overloading in PowerShell Well, you can overload it from C# code in this manner:

Overload * operator in python (or emulate it)

不打扰是莪最后的温柔 提交于 2019-12-04 14:47:14
I want to overload the * operator in python. In C++, you can overload the dereference operator, so that you can create a class with a custom way to respond to *alpha . Part of this question is that I don't know exactly, and I mean EXACTLY, what the * operator (unpacking operator as I call it) does. So how can I overload it, or emulate the overloading of it. Eventually I want to be able to do: *alpha with a custom response and return value. EDIT: I found the solution thanks to Joe Kington's comment. As *alpha unpacks according to __iter__ , so I defined a simple class that can be inherited from

How to use complex number “i” in C++

☆樱花仙子☆ 提交于 2019-12-04 13:10:18
问题 I am coding a simple DFT algorithm now and I want to use the complex number i in complex exponential. I saw somebody use #include<complex> and #include<cmath> , and then they used the overloaded symbol I such as exp(2*I) . But it seems it doesn't work in my visual studio compiler. So, can anyone give a simple example of using complex exponential? Thanks! 回答1: Here is a short complete example: #include <iostream> #include <complex> #include <cmath> using namespace std; typedef complex<double>

Overload C/C++ preprocessor macro on structure of its argument

五迷三道 提交于 2019-12-04 12:16:32
问题 I would like to write a preprocessor macro that does one thing if it's argument is a parenthesized tuple of tokens, like this: MY_MACRO((x, y)) and something else if it's just a single token, like this: MY_MACRO(x) Is that possible? How about distinguishing between the number of space-separated tokens, i.e. between MY_MACRO(x) and MY_MACRO(x y) ? Note that I am not trying to overload based on the number of arguments - it's a unary macro in all cases. EDIT : I am willing to use variadic macros

Why does Scala type inference fail here?

最后都变了- 提交于 2019-12-04 11:04:13
问题 I have this class in Scala: object Util { class Tapper[A](tapMe: A) { def tap(f: A => Unit): A = { f(tapMe) tapMe } def tap(fs: (A => Unit)*): A = { fs.foreach(_(tapMe)) tapMe } } implicit def tapper[A](toTap: A): Tapper[A] = new Tapper(toTap) } Now, "aaa".tap(_.trim) doesn't compile, giving the error error: missing parameter type for expanded function ((x$1) => x$1.trim) Why isn't the type inferred as String ? From the error it seems that the implicit conversion does fire (otherwise the