473,385 Members | 1,863 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,385 software developers and data experts.

template instantiation trouble

Hello,

I wrote an (templatized) operator==() and don't know why the compiler
doesn't consider it. What prevents it from being chosen?

class Data {};

template <typename D>
class Vector
{
public:
typedef Data ConstType;
typedef Data Type;
};

template <typename D>
bool
operator==(const typename Vector<D>::ConstType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

// this operator is chosen, if added.
//bool
//operator==(const Vector<Data>::ConstType &lhs,
// const Vector<Data>::Type &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::ConstType ct;
Vector<Data>::Type t;

ct == t;

return 0;
}
What is not standard conforming, what is not allowed here?

regards,
alex
Jul 22 '05 #1
5 1334

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:3f******@news.uni-ulm.de...
Hello,

I wrote an (templatized) operator==() and don't know why the compiler
doesn't consider it. What prevents it from being chosen?

class Data {};

template <typename D>
class Vector
{
public:
typedef Data ConstType;
typedef Data Type;
};

template <typename D>
bool
operator==(const typename Vector<D>::ConstType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

// this operator is chosen, if added.
//bool
//operator==(const Vector<Data>::ConstType &lhs,
// const Vector<Data>::Type &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::ConstType ct;
Vector<Data>::Type t;

ct == t;

return 0;
}
What is not standard conforming, what is not allowed here?

regards,
alex


Do you mean to say that your template operator==() is not chosen even if the
non-template version is commented out? Or do you mean to say that if the
non-template version is not commented out that it is preferred over the
template version? If the latter is the case, that makes sense since
non-templates are always preferred over templates...
Jul 22 '05 #2
Dave wrote:
Do you mean to say that your template operator==() is not chosen even if
the
non-template version is commented out? Or do you mean to say that if the
non-template version is not commented out that it is preferred over the
template version? If the latter is the case, that makes sense since
non-templates are always preferred over templates...


Yes. That's absolutely clear. But I mean the first. If I have only the
template version, it is not chosen!
Jul 22 '05 #3

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:3f******@news.uni-ulm.de...
Hello,

I wrote an (templatized) operator==() and don't know why the compiler
doesn't consider it. What prevents it from being chosen?

class Data {};

template <typename D>
class Vector
{
public:
typedef Data ConstType;
typedef Data Type;
I assume that you've missed out typedef D Data?

why are Type and ConstType the same?
surely typedef const Data ConstType.
};

template <typename D>
bool
operator==(const typename Vector<D>::ConstType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

I think that someone else recently posted a v similar question.
I don't know why the compiler doesn't consider it but I do know that even
attempting this is logically the wrong way to go for a number of reasons:

1. typedefs do not intoduce new types - they are only aliases for existing
types therefore all instantiations actually have the same signature i.e.
operator==(const Data&,const Data&) i.e. you cannot distinguish between
equality of Data when used in a Vector and when used in any other way.

2. The equality of Data is logically a part of the interface of Data not of
Vector - it is not the responsibility of a vector to define operations for
its parameters.

3. The whole idea is fundamantally wrong - If Data is a parameter and hence
you don't know what it is then you cannot logically define equality for it.
// this operator is chosen, if added.
//bool
//operator==(const Vector<Data>::ConstType &lhs,
// const Vector<Data>::Type &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::ConstType ct;
Vector<Data>::Type t;

ct == t;

return 0;
}
What is not standard conforming, what is not allowed here?

regards,
alex

Jul 22 '05 #4

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:3f******@news.uni-ulm.de...
Hello,

I wrote an (templatized) operator==() and don't know why the compiler
doesn't consider it. What prevents it from being chosen?

class Data {};

template <typename D>
class Vector
{
public:
typedef Data ConstType;
typedef Data Type;
};

template <typename D>
bool
operator==(const typename Vector<D>::ConstType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

// this operator is chosen, if added.
//bool
//operator==(const Vector<Data>::ConstType &lhs,
// const Vector<Data>::Type &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::ConstType ct;
Vector<Data>::Type t;

ct == t;

return 0;
}
What is not standard conforming, what is not allowed here?

regards,
alex


Here's the best I could find:

On page 170 of "C++ Templates", the following bullet item occurs and lists
one particular construct that is not a so-called "deduced context":

"Qualified type names. A type name like Q<T>::X will never be used to
deduce a template parameter T, for example."
Jul 22 '05 #5
You're absolutely right. That obvious yet that hidden.
I will rethink the design. That probably even makes things easier :-).
Thanks!
Jul 22 '05 #6

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

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
3
by: Patrick Guio | last post by:
Hi, I have trouble to compile the following piece of code with g++3.4 but not with earlier version // Foo.h template<typename T> class Foo { public:
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: Pallav singh | last post by:
Hi All, i am getting error during explicit function Instantiation for a class Template if i do explicit Instantiation of class it work and all function symbol i get in object file But if i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.