Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operators, implicit conversion and templates

Reply
Thread Tools

Operators, implicit conversion and templates

 
 
Christophe Poucet
Guest
Posts: n/a
 
      03-25-2005
Hello,

I have a few questions concerning automatic conversion. Apparently I
seem to be having an issue in that in certain cases it works and in
certain cases not. Could anyone perhaps tell me why the 4th test case
does not automatically convert while it shows in the 5th case that
these types are equivalent.
Basically the ddt template is to simulate a pointer or an array and
the var template is just a wrapper I use.

With regards,
Christophe Poucet

---------------------- CODE --------------------------
#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; }

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; }
private:
T* ptr_;
};

int main() {
{
var<int>* x = new var<int>[1];
x[0] = 1; // works
}
{
ddt<var<int> > x = new var<int>[1];
x[0] = 1; // Works
}
{
var<var<int>*> x = new var<int>[1];
x[0] = 1; // Works
}
{
var<ddt<var<int> > > x = new var<int>[1];
x[0] = 1; // Does not work
}
{
var<ddt<var<int> > > x = new var<int>[1];
static_cast<ddt<var<int> > >(x)[0] = 1; // Works
}
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implicit char conversion in templates moorep312 C++ 0 11-11-2010 09:01 AM
Implicit type conversion in combination with templates tonvandenheuvel@gmail.com C++ 2 12-13-2007 04:16 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates: "implicit typename is deprecated" error and typedef'ing templates Generic Usenet Account C++ 3 07-14-2005 08:02 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments