overload-resolution

c# overload resolution rules

家住魔仙堡 提交于 2019-12-12 05:49:05
问题 suppose the following extension methods: public static string ToFooBarString(this object obj) { ... } public static string ToFooBarString< T >(this IEnumerable< T > obj) { ... } Now i call this over a implementation of the IEnumerable< T > interface, say Dictionary< int , string > f; // implements IEnumerable< KeyValuePair< int , string > > f.ToFooBarString(); // <--- which one is called? which one is called in this case and why? 回答1: The compiler chooses the overload "closest" to the type in

Function template argument deduction with variadic class template as function call parameter

偶尔善良 提交于 2019-12-11 14:28:53
问题 All the examples are from here and here. Specifically, template<class...> struct Tuple { }; template< class... Types> void g(Tuple<Types ...>); // #1 // template<class T1, class... Types> void g(Tuple<T1, Types ...>); // #2 template<class T1, class... Types> void g(Tuple<T1, Types& ...>); // #3 g(Tuple<>()); // calls #1 g(Tuple<int, float>()); // calls #2 g(Tuple<int, float&>()); // calls #3 g(Tuple<int>()); // calls #3 With #2 uncommented, g()'s are resolved as described in the comments.

problems with overload resolution and operator<< for templated types

痴心易碎 提交于 2019-12-11 05:22:48
问题 I'm writing a library and it should stringify objects. I've chosen to support operator<<(ostream&... . Another thing is that my library should provide default stringification of types that don't have operator<<(ostream&... in the {?} form. The problem is with templated types like vector<> - I don't want the user to write 2 overloads for vector<int> and vector<float> - but I cannot get it to work. Here is the code: #include <string> #include <type_traits> #include <sstream> #include <vector>

“ambiguous overload for 'operator<<'” *without* a catch-all overload

人盡茶涼 提交于 2019-12-11 04:56:59
问题 So I am trying to implement the xorshift PRNGs as a parameterised STL-style class from random , like e.g. std::mersenne_twister_engine , so I can use it with those quite convenient distributions from the random library etc. Anyway, I have a problem overloading the operator<< and, frankly, I am completely stumped. The class is parameterised like this: template <size_t __n, int_least8_t __a, int_least8_t __b, int_least8_t __c, uint64_t __m> class xorshift_engine { ... The overload is declared

Disable default template and only use specialization through sfinae

拟墨画扇 提交于 2019-12-11 03:09:13
问题 Consider the following system: template<typename T> struct wrapper { operator T * () { return nullptr; } }; template<typename Ret, typename T> Ret func(T); template<> int func(float * in) { std::cout << "long"; } template<> long func(float * in) { std::cout << "int"; } The purpose of the wrapper is to allow it to decay to the type it is templated to (it is a wrapper around a buffer of the type). Moreover, i have a set of functions that are templated specializations of a template. This is to

Non virtual method resolution - why is this happening

谁说我不能喝 提交于 2019-12-11 02:29:29
问题 My understanding (in C#) of how non-virtual methods are resolved is that it is dependent upon the type of the variable (and not the type of instance). Take a look at the code below. class Program { static void Main(string[] args) { Sedan vehicle = new Sedan(); vehicle.Drive(); vehicle.Accelerate(); } } abstract class VehicleBase { public void Drive() { ShiftIntoGear(); Accelerate(); Steer(); } protected abstract void ShiftIntoGear(); protected abstract void Steer(); public void Accelerate() {

template instantiation with multiple template inheritance

馋奶兔 提交于 2019-12-11 02:09:18
问题 What are the rules for template instantiation when we pass a (multi)derived class to a template function expecting base class? For example: #include <iostream> template <int x> struct C {}; struct D : C<0>, C<1> {}; template <int x> void f (const C<x> &y) { std::cout << x << "\n"; } int main () { f (D ()); } MSVC 2015 prints 0, clang 3.8 - 1 and gcc 6.2 gives compiler error (Demo). And even if you SFINAE-away all overloads except one, the result will still be different: #include <iostream>

C++: avoid automatic conversion of string to bool in overloads

老子叫甜甜 提交于 2019-12-11 00:37:34
问题 I want to create a set of methods that will output a value with special formatting based on its type. When I'm doing it this way, it looks so far so good: static void printValue(std::ostringstream& out, int value) { out << value; } static void printValue(std::ostringstream& out, double value) { out << value; } static void printValue(std::ostringstream& out, const std::string& value) { out << "\"" << escapeString(value) << "\""; } Testing: printValue(std::cout, 123); // => 123 printValue(std:

C++11 lvalue, rvalue and std::move()

帅比萌擦擦* 提交于 2019-12-10 17:36:04
问题 I have the following code: #include <iostream> using namespace std; void test(int& a) { cout << "lvalue." << endl; } void test(int&& a) { cout << "rvalue" << endl; } int main(int argc, char *argv[]) { int a = 1; int&& b = 2; test(a); test(1); test(std::move(a)); test(b); } which outputs: lvalue. rvalue lvalue. lvalue. std::move() and int&& are rvalue references, I wonder why test(std::move(a)) and test(b) output lvalue ? Is it related with signature matching and function overloading? 回答1: The

assignment operator on empty inizializer_list

让人想犯罪 __ 提交于 2019-12-10 14:41:19
问题 can you explain how STL containers handle assignment operator with empty initializer list? when i'll do something like this: vector<int> v; v = { }; the function that is called is not : vector& operator= (initializer_list<value_type> il); but: vector& operator= (vector&& x); on the other hand, when i'll do something similar with my own class: struct A { A& operator= (const A&) { return *this; } A& operator= (A&&) { return *this; } A& operator= (initializer_list<int>) { return *this; } }; /* .