template instantiation trouble

A

Alexander Stippler

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
 
D

Dave

Alexander Stippler said:
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...
 
N

Nick Hounsome

Alexander Stippler said:
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.
 
A

Alexander Stippler

Dave said:
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!
 
D

Dave

Alexander Stippler said:
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."
 
A

Alexander Stippler

You're absolutely right. That obvious yet that hidden.
I will rethink the design. That probably even makes things easier :).
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top