conversion-operator

Is it possible to call templated user-defined conversion operator with explicit template arguments?

三世轮回 提交于 2020-08-20 05:39:10
问题 Lets consider the following code (compiles successfully with clang++ 7.0.0 , compiler arguments are -std=c++17 -Wall -Wextra -Werror -pedantic-errors ): #include <iostream> struct Foo { template <typename Type = void> operator int() { return 42; } }; int main() { const auto i = Foo{}.operator int(); std::cout << i << std::endl; } Is it possible to call such templated user-defined conversion operator with explicitly provided template arguments? The naive approach doesn't compile: const auto i

Is it possible to call templated user-defined conversion operator with explicit template arguments?

懵懂的女人 提交于 2020-08-20 05:38:19
问题 Lets consider the following code (compiles successfully with clang++ 7.0.0 , compiler arguments are -std=c++17 -Wall -Wextra -Werror -pedantic-errors ): #include <iostream> struct Foo { template <typename Type = void> operator int() { return 42; } }; int main() { const auto i = Foo{}.operator int(); std::cout << i << std::endl; } Is it possible to call such templated user-defined conversion operator with explicitly provided template arguments? The naive approach doesn't compile: const auto i

C++ conversion operator to chrono::duration - works with c++17 but not C++14 or less

微笑、不失礼 提交于 2020-01-03 08:35:30
问题 The following code compiles with gcc 7.1.0 with C++17 set but does not compile with C++14 set (or Visual Studio 2017). It is easy to reproduce on Wandbox. What has to be done to make it work with C++11/14? #include <iostream> #include <chrono> int main() { struct Convert { operator std::chrono::milliseconds() { std::cout << "operator std::chrono::milliseconds" << std::endl; return std::chrono::milliseconds(10); } operator int64_t () { std::cout << "operator int64_t" << std::endl; return 5; }

explicit copy constructor or implicit parameter by value

孤人 提交于 2019-12-30 00:07:10
问题 I recently read (and unfortunately forgot where), that the best way to write operator= is like this: foo &operator=(foo other) { swap(*this, other); return *this; } instead of this: foo &operator=(const foo &other) { foo copy(other); swap(*this, copy); return *this; } The idea is that if operator= is called with an rvalue, the first version can optimize away construction of a copy. So when called with a rvalue, the first version is faster and when called with an lvalue the two are equivalent.

Since there are two ways to define a conversion in C++ how do they interact when there are two possibilities for the same conversion?

不想你离开。 提交于 2019-12-21 12:08:30
问题 I am just looking for clarification on how C++ works, this isn't really about solving a particular problem in my code. In C++ you can say that type A should implicitly convert to type B in two different ways. If you are the author of A you can add something like this to A: operator B() { // code } If you are the author of B you can add something like this to B: B(const A &a) { // code } Either one of these, if I understand correctly, will allow A to implicitly convert to B. So if both are

clang++ fails but g++ succeeds on using a cast to const-unrelated-type operator in an assignment

≡放荡痞女 提交于 2019-12-21 09:21:42
问题 Here's a short example that reproduces this “no viable conversion” with lemon for clang but valid for g++ difference in compiler behavior. #include <iostream> struct A { int i; }; #ifndef UNSCREW_CLANG using cast_type = const A; #else using cast_type = A; #endif struct B { operator cast_type () const { return A{i}; } int i; }; int main () { A a{0}; B b{1}; #ifndef CLANG_WORKAROUND a = b; #else a = b.operator cast_type (); #endif std::cout << a.i << std::endl; return EXIT_SUCCESS; } live at

Explicit ref-qualified conversion operator templates in action

五迷三道 提交于 2019-12-21 04:27:17
问题 Given the following conversion operators struct A { template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () &; template<typename T> explicit operator const T& () const&; }; struct B {}; I would expect the following conversions to be all valid, yet some give compile errors (live example): A a; A&& ar = std::move(a); A& al = a; const A& ac = a; B&& bm(std::move(a)); // 1. OK B&& bt(A{}); // 2. OK B&& br(ar); // 3. error: no viable conversion from A to B B&