Implicit conversion issue

C

Christophe Poucet

Hellom

I have an issue with implicit conversions. Apparently when one calls
an operator [] on a class Y which has a conversion operator to class X
which has the operator []. Sadly it will not do implicit conversion
for this case, however if instead of class X we just use a pointer
then the implicit conversion will work.

Anyone have any ideas why it will not do implicit conversion?

A complete test case is given below.

------------------------------------------
include <cstdio>
using namespace std;
template <typename T>
class var {
public:
var() {}

var(T data) : data_(data) {}

template <typename T2>
var(T2 data) : data_(data) {}

operator const T&() const { return data_; }

template <typename T2> var & operator= (const T2& data) { data_ =
data; return * this}

private:
T data_;
};

template <typename T>
class ddt{
public:
ddt(T* ptr) : ptr_(ptr) {}

T& operator[](const size_t n) { return ptr_[n]; }

const T& operator[](const size_t n) const { return ptr_[n]; }

ddt & operator=(T* ptr) { ptr_ = ptr; return *this; }
private:
T* ptr_;
};

int main() {
{
var<int>* x = new var<int>[1];
x[0] = 1;
}
{
ddt<var<int> > x = new var<int>[1];
x[0] = 1;
}
{
var<var<int>*> x = new var<int>[1];
x[0] = 1; // Implicit conversion to var<int>* works
}
{
var<ddt<var<int> > > x = new var<int>[1];
x[0] = 1; // Implicit conversion to ddt<var<int> > does NOT work
}
return 0;
}

// g++ -O3 -o templatetest templatetest.cpp
// templatetest.cpp: In function `int main()':
// templatetest.cpp:50: no match for `var<ddt<var<int> > >& [int]'
operator
// make: *** [templatetest] Error 1
 
V

Victor Bazarov

Christophe said:

Hellom tom youm toom.
I have an issue with implicit conversions. Apparently when one calls
an operator [] on a class Y which has a conversion operator to class X
which has the operator [].

No conversions are applied to an object to find if the converted type
has a matching function. Example:

struct A { void foo(); };
struct B { operator A(); };
int main() {
B b;
b.foo(); // 'foo' is not found in B although it could be
// found had 'b' been converted to an A.
}

Victor
Sadly it will not do implicit conversion
for this case, however if instead of class X we just use a pointer
then the implicit conversion will work.

Anyone have any ideas why it will not do implicit conversion?

Because the language doesn't require it. Because it would potentially
need to try an infinite number of conversions in an attempt to find the
solution. That's just not how it works.
A complete test case is given below.

Actually, it's incomplete.

You probably meant

#include said:
using namespace std;
template <typename T>
class var {
public:
var() {}

var(T data) : data_(data) {}

template <typename T2>
var(T2 data) : data_(data) {}

operator const T&() const { return data_; }

template <typename T2> var & operator= (const T2& data) { data_ =
data; return * this}

There is a missing semicolon after 'this'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top