By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Implicit conversion issue

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.