473,769 Members | 6,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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==(cons t typename Vector<D>::Cons tType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

// this operator is chosen, if added.
//bool
//operator==(cons t Vector<Data>::C onstType &lhs,
// const Vector<Data>::T ype &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::C onstType ct;
Vector<Data>::T ype t;

ct == t;

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

regards,
alex
Jul 22 '05 #1
5 1366

"Alexander Stippler" <st**@mathemati k.uni-ulm.de> wrote in message
news:3f******@n ews.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==(cons t typename Vector<D>::Cons tType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

// this operator is chosen, if added.
//bool
//operator==(cons t Vector<Data>::C onstType &lhs,
// const Vector<Data>::T ype &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::C onstType ct;
Vector<Data>::T ype 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**@mathemati k.uni-ulm.de> wrote in message
news:3f******@n ews.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==(cons t typename Vector<D>::Cons tType &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==(cons t 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==(cons t Vector<Data>::C onstType &lhs,
// const Vector<Data>::T ype &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::C onstType ct;
Vector<Data>::T ype t;

ct == t;

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

regards,
alex

Jul 22 '05 #4

"Alexander Stippler" <st**@mathemati k.uni-ulm.de> wrote in message
news:3f******@n ews.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==(cons t typename Vector<D>::Cons tType &lhs,
const typename Vector<D>::Type &rhs)
{
return true;
}

// this operator is chosen, if added.
//bool
//operator==(cons t Vector<Data>::C onstType &lhs,
// const Vector<Data>::T ype &rhs)
//{
// return true;
//}

int
main()
{
Vector<Data>::C onstType ct;
Vector<Data>::T ype 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
2020
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 instantiation of foo:
7
2480
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
4
1652
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
8257
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
2623
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
2020
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
3
2737
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 template class called "myTemplate", for which there is an instantiation for 'int' type. Now suppose a class in module B binary wants to use this template
8
2144
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 cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
2
2606
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 I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library #include <blitz/array.h>...
4
1599
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 try to expose only one function of my class its failing #include <iostream>
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.