overloading

Trouble with const/non-const overload resolution

不羁岁月 提交于 2019-12-03 12:18:37
问题 I have a class that looks something like this: class ClassA { public: float Get(int num) const; protected: float& Get(int num); } Outside of the class, I call the Get() function. float foo = classAInstance.Get(i); I expect this to call the public version, but instead Visual Studio errors out: error C2248: 'ClassA::Get' : cannot access protected member declared in class 'ClassA' When commenting out the protected overload and removing all references to it, the code compiles. Why does the

Methods overloading with value and reference parameter types

只谈情不闲聊 提交于 2019-12-03 12:00:58
I have the following code : class Calculator { public int Sum(int x, int y) { return x + y; } public int Sum(out int x, out int y) { x = y = 10; return x + y; } } class Program { static void Main(string[] args) { int x = 10, y = 20; Calculator calculator = new Calculator(); Console.WriteLine ( calculator.Sum ( x , y ) ); Console.WriteLine ( calculator.Sum ( out x , out y ) ); } } This code work well despite that methods signature are differenciated only be the out keyword. But the following code didn't work : class Calculator { public int Sum(ref int x, ref int y) { return x + y; } public int

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

泪湿孤枕 提交于 2019-12-03 11:37:32
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? If you change the template function signatures a little bit, it still works (as I would expect given

Method overloading return values [duplicate]

孤人 提交于 2019-12-03 11:15:04
问题 This question already has answers here : Function overloading by return type? (14 answers) Why can't two methods be declared with the same signature even though their return types are different? [duplicate] (5 answers) Closed 6 years ago . In C# I need to be able to define a method but have it return one or two return types. The compiler gives me an error when I try to do it, but why isn't it smart enough to know which method I need to call? int x = FunctionReturnsIntOrString(); Why would the

When is deleting a template instantiation preferable to deleting a non-template overload?

怎甘沉沦 提交于 2019-12-03 11:04:08
问题 Suppose I have a template that works with raw pointers: template<typename T> void processPointer(T* ptr); I don't want this to be called with void* pointers. It seems I have two choices. I can delete a non-template overload: void processPointer(void*) = delete; Or I can delete a template instantiation: template<> void processPointer<void>(void*) = delete; Declaring the non-template overload is easier (no futzing with angle brackets). Are there reasons why I'd prefer to delete the template

C++ return type overload hack

China☆狼群 提交于 2019-12-03 09:11:02
问题 I was bored and came up with such hack (pseudocode): 1 struct proxy { 2 operator int(); // int function 3 operator double(); // double function 4 proxy(arguments); 5 arguments &arguments_; 6 }; 7 8 proxy function(arguments &args) { 9 return proxy(args); 10 } 11 int v = function(...); 12 double u = function(...); is it evil to use in real code? my possible usage scenario is for example product of array elements, which may/may not overflow: int size(short *array); short size(short *array); The

Overloaded constructor calling other constructor, but not as first statement

≡放荡痞女 提交于 2019-12-03 08:51:37
问题 I'm having some trouble using multiple constructors in java. what I want to do is something like this: public class MyClass { // first constructor public MyClass(arg1, arg2, arg3) { // do some construction } // second constructor public MyClass(arg1) { // do some stuff to calculate arg2 and arg3 this(arg1, arg2, arg3); } } but I can't, since the second constructor cannot call another constructor, unless it is the first line. What is the common solution for such situation? I can't calculate

Why was function overloading added to C++?

こ雲淡風輕ζ 提交于 2019-12-03 08:16:37
问题 I have a C background. I was just wondering why was function overloading added to C++? C doesn't have function overloading but C++ does, what was the need for it? What went across the mind of the language designer at that time? 回答1: It increases maintainability. If you have a type T and you call a function with it, then you need to change T, if the function has been overloaded for the new T then you can recompile instantly. In C you would have to go back and dig through all the call sites and

How is <tgmath.h> implemented?

社会主义新天地 提交于 2019-12-03 08:16:14
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? rlbond 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 do primitive and user-defined types act differently when returned as 'const' from a function?

爱⌒轻易说出口 提交于 2019-12-03 06:29:03
问题 #include <iostream> using namespace std; template<typename T> void f(T&&) { cout << "f(T&&)" << endl; } template<typename T> void f(const T&&) { cout << "f(const T&&)" << endl; } struct A {}; const A g1() { return {}; } const int g2() { return {}; } int main() { f(g1()); // outputs "f(const T&&)" as expected. f(g2()); // outputs "f(T&&)" not as expected. } The issue description is embedded in the code. My compiler is clang 5.0 . I just wonder: Why does C++ treat built-in types and custom