overloading

Priority between normal function and Template function

a 夏天 提交于 2019-12-01 04:48:12
In the following code, the main function uses normal function instead of Template function. #include <iostream> using namespace std; template <class T> void num(T t){cout<<"T : "<<t;} void num(int a){cout<<"wT : "<<a;} int main() { num(5); return 0; } What is the possible reason behind this? To call the template method in this case, you need to invoke the method explicitly with num<int>(5) instead of num(5) . Although the compiler can infer, non-generic method is preferred to a generic one. You can take a look at this behavior here http://ideone.com/ccDJP . Take a look at Herb Sutter's

C++ templated function overloading rules

可紊 提交于 2019-12-01 04:40:21
问题 When overloading a templated function, how should the compiler chose which version of the function to call if it has the option to either: Call a templated version of the function (such as func<T>(foo) ). Call an overloaded version of the function which is not itself templated but where the type of the parameter being passed to the function inherits from the type specified in the overloaded function template. Consider the following C++ code: #include <stdio.h> struct Parent {}; struct Child :

Is it possible to bring global function into the overload resolution with member function?

淺唱寂寞╮ 提交于 2019-12-01 04:30:51
Here is the corresponding question, what I want to know is, Is it possible to bring global function into the overload resolution with member function? I tried in 2 ways, but both don't work: void foo(double val) { cout << "double\n";} class obj { public: using ::foo; // (1) compile error: using-declaration for non-member at class scope void callFoo() { using ::foo; // (2)will cause the global version always be called foo(6.4); foo(0); } private: void foo(int val) {cout << "class member foo\n"; } }; This is plain old unqualified name lookup, specified in §3.4.1 [basic.lookup.unqual]: 1 In all

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

此生再无相见时 提交于 2019-12-01 03:52:46
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 ambiguous main.cpp:15:33: note: candidates are: main.cpp:6:6: note: void f(bool) main.cpp:10:6: note:

C++11: Overload fails to resolve recursive decltype

牧云@^-^@ 提交于 2019-12-01 03:41:51
In the following piece of code, I'm trying to build a lattice of types. For instance, between float and int , promote the result to float : float join(float f, int) { return f; } float join(float f, float) { return f; } Then I introduce a wrapper type: template <typename Inner> struct wrapper { using inner_t = Inner; inner_t value; }; whose behavior with the join operation quite natural: template <typename Inner1, typename Inner2> auto join(const wrapper<Inner1>& w1, const wrapper<Inner2>& w2) -> wrapper<decltype(join(w1.value, w2.value))> { return {join(w1.value, w2.value)}; } It can also be

overloading with both widening and boxing

六月ゝ 毕业季﹏ 提交于 2019-12-01 03:39:23
问题 public void add(long... x){} public void add(Integer... x){} add(2); this produces error...why overlaoding is not performed with both widening and boxing? but overloading without vararg works fine public void add(long x){} public void add(Integer x){} add(2); here add(long x) will be executed that is widening beats boxing...why not same concept with var arguments 回答1: Java compiler performs three attempts to choose an appropriate method overload (JLS §15.12.2.1): Phase 1: Identify Matching

C++ std::stringstream operator<< overloading

谁说胖子不能爱 提交于 2019-12-01 03:27:48
I have the following class(prototipe): class Token { public: //members, etc. friend std::stringstream& operator<< (std::stringstream &out, Token &t); }; And the operator is implemented like this: std::stringstream & operator<< (std::stringstream &out, Token &t) { out << t.getValue(); //class public method return out; } Now, I'm trying to use it like this: std::stringstream out; Token t; //initialization, etc. out << t; And VS gives me error, saying that there is no match for << operator. What am I wrong in? std::stringstream & operator<< (std::stringstream &out, Token &t) should be std:

Why is the compiler choosing this template function over an overloaded non-template function?

北城余情 提交于 2019-12-01 03:23:30
Using VC++ 2010, given the following: class Base { }; class Derived : public Base { }; template<class T> void foo(T& t); // A void foo(Base& base); // B Derived d; foo(d); // calls A foo(static_cast<Base&>(d)); // calls B I would like "B" to be called above. I can achieve this with a cast to Base , but why is this necessary? I want the template function to be called for all types not derived from Base (built-in types, etc.), but I want the non-template overload to be called for types derived from Base , without requiring the client to explicitly cast. I also tried making the overload a

Using __set with arrays solved, but why?

人走茶凉 提交于 2019-12-01 03:10:15
问题 Having done a bit of research, I eventually came across the answer to a question I was soon to ask here anyways; How do you work with arrays via the __get and __set magic methods in PHP? Whenever I was trying to set a value using something like $object->foo['bar'] = 42; it seemed to silently discard it. Anyways, the answer is simple; The __get method simply needs to return by reference. And after tossing an ampersand in front of it, sure enough it works. My question actually, is why? I can't

Is it possible to bring global function into the overload resolution with member function?

北城余情 提交于 2019-12-01 02:51:29
问题 Here is the corresponding question, what I want to know is, Is it possible to bring global function into the overload resolution with member function? I tried in 2 ways, but both don't work: void foo(double val) { cout << "double\n";} class obj { public: using ::foo; // (1) compile error: using-declaration for non-member at class scope void callFoo() { using ::foo; // (2)will cause the global version always be called foo(6.4); foo(0); } private: void foo(int val) {cout << "class member foo\n"