overload-resolution

How does template argument deduction work when an overloaded function is involved as an argument?

孤人 提交于 2019-12-19 05:12:57
问题 This is the more sophisticated question mentioned in How does overload resolution work when an argument is an overloaded function? Below code compiles without any problem: void foo() {} void foo(int) {} void foo(double) {} void foo(int, double) {} // Uncommenting below line break compilation //template<class T> void foo(T) {} template<class X, class Y> void bar(void (*f)(X, Y)) { f(X(), Y()); } int main() { bar(foo); } It doesn't appear a challenging task for template argument deduction -

Why is a public const method not called when the non-const one is private?

前提是你 提交于 2019-12-18 10:58:13
问题 Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; a.foo(); } The compiler error is: error: 'void A::foo()' is private`. But when I delete the private one it just works. Why is the public const method not called when the non-const one is private? In other words, why does overload resolution come before access control? This is strange. Do you think it is consistent? My code

Does SFINAE apply to function bodies?

帅比萌擦擦* 提交于 2019-12-18 08:58:35
问题 I have the following sample code: class Serializable {}; class MyData : public Serializable {}; void GetData( Serializable& ) {} template<typename T> void GetData( T& data ) { std::istringstream s{"test"}; s >> data; } int main() { MyData d; GetData(d); } (Live Sample) Based on overload resolution rules, the non-template version should be preferred because the base class is of type Serializable . However, I expect SFINAE to kick in when there are errors in the template version when it is

Obtaining address locations of an overload method

懵懂的女人 提交于 2019-12-18 04:12:27
问题 How do I get all the address locations for functions/procedures/methods that is overloaded? For example, Dialogs.MessageDlgPosHelp is overloaded having two different versions of it - one without a default button and one with. How would I obtain the address locations for the two functions? 回答1: Based on this thread and what Thomas Mueller pointed there, you might define types with the same signatures as methods whose addresses you want to obtain (for each overload). If you then declare the

Ambiguous call when recursively calling variadic template function overload

ぃ、小莉子 提交于 2019-12-18 03:42:12
问题 Consider this piece of code: template<typename FirstArg> void foo() { } template<typename FirstArg, typename... RestOfArgs> void foo() { foo<RestOfArgs...>(); } int main() { foo<int, int, int>(); return 0; } It does not compile due to ambiguous call foo<RestOfArgs...>(); when RestOfArgs has only one element ( {int} ). But this compiles without error: template<typename FirstArg> void foo(FirstArg x) { } template<typename FirstArg, typename... RestOfArgs> void foo(FirstArg x, RestOfArgs... y) {

Ambiguous call when recursively calling variadic template function overload

风格不统一 提交于 2019-12-18 03:41:09
问题 Consider this piece of code: template<typename FirstArg> void foo() { } template<typename FirstArg, typename... RestOfArgs> void foo() { foo<RestOfArgs...>(); } int main() { foo<int, int, int>(); return 0; } It does not compile due to ambiguous call foo<RestOfArgs...>(); when RestOfArgs has only one element ( {int} ). But this compiles without error: template<typename FirstArg> void foo(FirstArg x) { } template<typename FirstArg, typename... RestOfArgs> void foo(FirstArg x, RestOfArgs... y) {

Should this compile? Overload resolution and implicit conversions

拈花ヽ惹草 提交于 2019-12-18 03:08:04
问题 This example seems to compile with VC10 and gcc (though my version of gcc is very old). EDIT: R. Martinho Fernandez tried this on gcc 4.7 and the behaviour is still the same. struct Base { operator double() const { return 0.0; } }; struct foo { foo(const char* c) {} }; struct Something : public Base { void operator[](const foo& f) {} }; int main() { Something d; d["32"]; return 0; } But clang complains: test4.cpp:19:6: error: use of overloaded operator '[]' is ambiguous (with operand types

“Manual” signature overload resolution

我与影子孤独终老i 提交于 2019-12-18 01:05:56
问题 I want to make a std::function like object that can handle storing more than one overload. Syntax sort of like this: my_function< int(double, int), double(double, double), char(int, int) > . Or, more explicitly: template<typename... Ts> struct type_list {}; template<typename... Signatures > struct my_function { std::tuple< std::function<Signatures>... > m_functions; typedef type_list< Signatures... > sig_list; template<typename... Args> typename pick_overload_signature< sig_list, type_list

How does “std::cout << std::endl;” compile?

旧巷老猫 提交于 2019-12-17 20:17:40
问题 Most IO stream manipulators are regular functions with the following signature: std::ios_base& func( std::ios_base& str ); However some manipulators (including the most frequently used ones - std::endl and std::flush) are templates of the following form: template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& func(std::basic_ostream<CharT, Traits>& os); Then, how does the compilation of std::cout << std::endl; succeed given that the following example fails: $ cat main.cpp

C++0x const RValue reference as function parameter

∥☆過路亽.° 提交于 2019-12-17 16:43:13
问题 I am trying to understand why someone would write a function that takes a const rvalue reference . In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const Rvalue above the const LValue reference function (returning "2"). #include <string> #include <vector> #include <iostream> std::vector<std::string> createVector() { return std::vector<std::string>(); } //takes movable rvalue void func(std::vector<std