468,777 Members | 2,364 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,777 developers. It's quick & easy.

Implicit conversion issue

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
Jul 23 '05 #1
1 1635
Christophe Poucet wrote:
Hellom
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.

------------------------------------------
include <cstdio>
You probably meant

#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}
There is a missing semicolon after 'this'.
[...]


V
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steve Gough | last post: by
25 posts views Thread by Chad Z. Hower aka Kudzu | last post: by
36 posts views Thread by Chad Z. Hower aka Kudzu | last post: by
1 post views Thread by jschon | last post: by
12 posts views Thread by robert bristow-johnson | last post: by
19 posts views Thread by =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post: by
8 posts views Thread by jonpb | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.