Created
November 16, 2017 09:43
-
-
Save oliora/1ac5ae6beba84623d44d0353cea6a26e to your computer and use it in GitHub Desktop.
It's a bit tricky to get constructor and assignment operator accepting forward reference (singular or variadic) right
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
#include <utility> | |
struct Bar {}; | |
// Note about `is_convertible<T&&, Foo>`. `T&&` is essential there! | |
struct Foo | |
{ | |
template<class T, class = typename std::enable_if<!std::is_convertible<T&&, Foo>::value>::type> | |
Foo(T&& t) {} | |
Foo(Bar&& bar) {} | |
template<class T, class = typename std::enable_if<!std::is_convertible<T&&, Foo>::value>::type> | |
Foo& operator= (T&& t) { return *this; } | |
Foo& operator= (Bar&& bar) { return *this; } | |
}; | |
// Type your code here, or load an example. | |
int square(int num) { | |
Bar bar1, bar2; | |
Foo foo1(bar1); | |
Foo foo2(std::move(bar1)); | |
foo1 = bar2; | |
foo2 = std::move(bar2); | |
(void) foo1; (void) foo2; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment