conversion-operator

Const operator overloading problems in C++

╄→гoц情女王★ 提交于 2019-12-12 16:19:45
问题 I'm having trouble with overloading operator() with a const version: #include <iostream> #include <vector> using namespace std; class Matrix { public: Matrix(int m, int n) { vector<double> tmp(m, 0.0); data.resize(n, tmp); } ~Matrix() { } const double & operator()(int ii, int jj) const { cout << " - const-version was called - "; return data[ii][jj]; } double & operator()(int ii, int jj) { cout << " - NONconst-version was called - "; if (ii!=1) { throw "Error: you may only alter the first row

static_cast vs. direct call to conversion operator?

∥☆過路亽.° 提交于 2019-12-07 10:27:05
问题 Consider the following class, just as a simple example: #include <iostream> #include <string> using namespace std; class point { public: int _x{ 0 }; int _y{ 0 }; point() {} point(int x, int y) : _x{ x }, _y{ y } {} operator string() const { return '[' + to_string(_x) + ',' + to_string(_y) + ']'; } friend ostream& operator<<(ostream& os, const point& p) { // Which one? Why? os << static_cast<string>(p); // Option 1 os << p.operator string(); // Option 2 return os; } }; Should one call a

operator T() not used in assignment

≡放荡痞女 提交于 2019-12-07 05:06:17
问题 I am a bit confused by this. Lets assume I have a helper class Data class Data { public: Data(const QVariant &value) : m_Variant(value) { } operator QString() const { return m_Variant.toString(); } private: QVariant m_Variant; }; then when I do this: Data d("text"); QString str = d; //str becomes "text" it works but when I continue to do: Data d2("text2"); str = d2; //does not compile it fails complaining: ambiguous overload for 'operator=' (operand types are 'QString' and 'Data') candidates

Template conversion operator difference between clang 6 and clang 7

本秂侑毒 提交于 2019-12-05 21:28:10
问题 I have some code that uses template conversion operator to find return type of function found through ADL. The simplified code look like this: #include <type_traits> template<typename S> struct probe { template<typename T, typename U = S, std::enable_if_t< std::is_same<T&, U>::value && !std::is_const<T>::value, int> = 0> operator T& (); template<typename T, typename U = S&&, std::enable_if_t< std::is_same<T&&, U>::value && !std::is_const<T>::value, int> = 0> operator T&& (); template<typename

static_cast vs. direct call to conversion operator?

ぐ巨炮叔叔 提交于 2019-12-05 12:55:29
Consider the following class, just as a simple example: #include <iostream> #include <string> using namespace std; class point { public: int _x{ 0 }; int _y{ 0 }; point() {} point(int x, int y) : _x{ x }, _y{ y } {} operator string() const { return '[' + to_string(_x) + ',' + to_string(_y) + ']'; } friend ostream& operator<<(ostream& os, const point& p) { // Which one? Why? os << static_cast<string>(p); // Option 1 os << p.operator string(); // Option 2 return os; } }; Should one call a conversion operator directly, or rather just call static_cast and let that do the job? Those two lines will

operator T() not used in assignment

限于喜欢 提交于 2019-12-05 10:14:31
I am a bit confused by this. Lets assume I have a helper class Data class Data { public: Data(const QVariant &value) : m_Variant(value) { } operator QString() const { return m_Variant.toString(); } private: QVariant m_Variant; }; then when I do this: Data d("text"); QString str = d; //str becomes "text" it works but when I continue to do: Data d2("text2"); str = d2; //does not compile it fails complaining: ambiguous overload for 'operator=' (operand types are 'QString' and 'Data') candidates are: ... QString &operator=(const QString &); QString &operator=(QString &&); QString &operator=(const

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-04 04:31:02
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 defined which one is used? Is that even allowed? NOTE: I understand that you should probably never be in a

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

喜夏-厌秋 提交于 2019-12-04 03:40:28
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 godbolt's g++ (4.9, 5.2) compiles that silently; whereas clang++ (3.5, 3.7) compiles it if using cast

Explicit ref-qualified conversion operator templates in action

天大地大妈咪最大 提交于 2019-12-03 13:09:24
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& bl(al); // 4. OK const B& bz(al); // 5. OK const B& bc(ac); // 6. OK B cm(std::move(a)); // 7. error

C++ overload resolution, conversion operators and const

不问归期 提交于 2019-12-01 20:23:34
问题 In this case void f(int *); void f(const int *); ... int i; f(&i); the situation is pretty clear - f(int *) gets called which seems right. However, if I have this (it was done like that by mistake(*) ): class aa { public: operator bool() const; operator char *(); }; void func(bool); aa a; func(a); operator char *() gets called. I cannot figure out why would such decision path be better than going to operator bool(). Any ideas? (*) If const is added to the second operator, the code works as