473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Operators, implicit conversion and templates

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
Jul 23 '05 #1
0 899

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
2
by: rasbury | last post by:
If I were to include in the definition of a string class the following operators: class String { public: // ... operator const char *() const; // conversion to C-style string char...
6
by: TB | last post by:
This is a repost of something I had posted four months ago with no response, I'm hoping to get some feedback... From the Microsoft .net Core Reference (pg. 104) C# shares the AND (short-circuit)...
3
by: Chris | last post by:
Hi, I want to convert an integer to an object of type 'MyNumber' using implicit conversions. MyNumber* pDt = 10; but it doesn't work ?? Here's what I did :
3
by: Egbert Nierop \(MVP for IIS\) | last post by:
Example... This would make that i can assign a ULONG to a CComBSTR2 class while the body of the code performs necessary conversion. CComBSTR2& operator=(ULONG ulong) {
10
by: andrew browning | last post by:
i have overlaoded all of my arithmetic operators but all are functioning as multiplication. below is a sample of the addition operator: Rational operator + (const Rational& r1, const Rational&...
11
by: Jim Michaels | last post by:
friend fraction& operator+=(const fraction& rhs); fraction.h(64) Error: error: 'fraction& operator+=(const fraction&)' must take exactly two arguments I practically pulled this out of a C++...
19
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary...
1
Frinavale
by: Frinavale | last post by:
In C# you can use the implicit keyword to define an implicit user-defined type conversion operator. In VB.NET you can define a CType conversion operator that will explicitly convert a user-defined...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.