overload-resolution

Partial ordering of function templates containing template parameter packs

三世轮回 提交于 2019-12-04 16:55:01
Partial ordering of function templates containing template parameter packs is independent of the number of deduced arguments for those template parameter packs. 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 The above is quoted from partial ordering of overload function templates

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

Mockito: Verifying overloaded methods with type-compatible arguments

倖福魔咒の 提交于 2019-12-04 09:49:59
Consider you want to mock an interface using Mockito containing the following method signatures: public void doThis(Object o); public void doThis(Object... o) I need to verify that doThis(Object o) (and not the other method) has been invoked exactly one time. First I thought that the following line would do the trick: verify(mock, times(1)).doThis(anyObject()); However, as this seems to work on Windows, it doesn't on Linux because in this environment, a call to of the other doThis method is expected. This is caused because the anyObject() argument seems to match both method signatures and one

Confusion around function call resolution

元气小坏坏 提交于 2019-12-04 04:42:00
This question is inspired by this one . Consider the code: namespace ns { template <typename T> void swap(T& a, T& b) { using namespace std; swap(a, b); } } After some test with GCC, I found that swap(a, b); resolves to 1) std::swap if T has overloaded std::swap (e.g., standard container types) 2) ns::swap otherwise, leading to infinite recursion. So, it seems that the compiler will first try to find a match in the namespace ns . If a match is found, the search ends. But this is not the case when ADL comes in, in which case, std::swap is found anyway. The resolution process seems to be

Resolution of virtual function with default parameters [duplicate]

只愿长相守 提交于 2019-12-04 01:26:57
问题 This question already has answers here : Can virtual functions have default parameters? (6 answers) Closed 5 years ago . header.h #include <iostream> using namespace std; class A { public: virtual void display(int i=5) { cout<< "Base::" << i << endl; } }; class B : public A { public: void display(int i=9) { cout<< "Derived::" << i << endl; } }; source.h #include <iostream> #include "header.h" using namespace std; int main() { A * a = new B(); a->display(); A* aa = new A(); aa->display(); B*

call of overloaded with ref-qualifiers member function is ambiguous

蓝咒 提交于 2019-12-04 01:21:15
问题 I found a strange behaviour, when compliling my code with G++ ( gcc 4.8.1 and MinGW 4.8.2 with -std=gnu++1y flag). In spirit of SSCCE I isolating the following snippet: struct C { template< typename X > auto f(X &&) const & { ; } template< typename X > auto f(X &&) & { ; } template< typename X > auto f(X &&) && { ; } }; int main() { int i{}; #if 1 C{}.f(i); #endif #if 1 C c{}; c.f(i); #endif return 0; } It gives an error: main.cpp: In function 'int main()': main.cpp:29:10: error: call of

Incorrect overload resolution for 2-argument functions

喜欢而已 提交于 2019-12-04 01:03:38
问题 Let's take the following example program: #include <cmath> namespace half_float { template<typename T> struct half_expr {}; struct half : half_expr<half> { operator float() const; }; template<typename T> half sin(const half_expr<T>&); template<typename T> half atan2(const half_expr<T>&, const half_expr<T>&); } using namespace std; using half_float::half; int main() { half a, b; half s = sin(a); half t = atan2(a, b); } In VS 2010 this compiles just fine (ignore the obvious linker errors for

Why does the compiler find my function if is not yet declared?

前提是你 提交于 2019-12-03 23:12:00
Contrary to my expectations, this program works: #include <iostream> namespace a { struct item{}; } namespace b { struct item{}; } template<typename T> void func(T t) { do_func(t); } int main() { func(a::item{}); func(b::item{}); } namespace a { void do_func(item) { std::cout << "a::func\n"; } } namespace b { void do_func(item) { std::cout << "b::func\n"; } } Output: a::func b::func Verifications with online compilers: gcc 4.8 clang 3.4 If the instantation of func<T> occurs in the body of main then I would expect that a::do_func and b::do_func are not yet declared. How can this work? Update

Overload resolution and partial template ordering

瘦欲@ 提交于 2019-12-03 22:31:32
Consider this simple pair of function templates. template <typename T> void foo(T& ) { std::cout << __PRETTY_FUNCTION__ << '\n'; } template <typename C> void foo(const C& ) { std::cout << __PRETTY_FUNCTION__ << '\n'; } If we call foo with a non-const argument: int i = 4; foo(i); The T& overload is preferred based on [over.ics.rank]/3.2.6, since the deduced reference int& is less cv -qualified than the deduced reference const int& . However, if we call foo with a const argument: const int ci = 42; foo(ci); The const C& overload is preferred because it is "more specialized" based on [over.match

Which to use: move assignment operator vs copy assignment operator

心已入冬 提交于 2019-12-03 16:08:13
I don't seem to get why would you use the move assignment operator : CLASSA & operator=(CLASSA && other); //move assignment operator over, the copy assignment operator : CLASSA & operator=(CLASSA other); //copy assignment operator The move assignment operator takes an r-value reference only e.g. CLASSA a1, a2, a3; a1 = a2 + a3; In the copy assignment operator , other can be constructor using a copy constructor or a move constructor (if other is initialized with an rvalue, it could be move-constructed --if move-constructor defined--). If it is copy-constructed , we will be doing 1 copy and that