473,808 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with explicit instantiation

> cat X.h
#ifndef GUARD_X_H
#define GUARD_X_H 1
#include <iostream>

template<class T>
class X {
private:
// representation
T D;
public:
// functions
T data(void) const { return D; }
// constructors
X(T d = 0): D(d) { }
X(const X& x);
template<class S>
X(const X<S>& x);
friend
std::ostream& operator<<(
std::ostream& os, const X<T>& x) {
return os << x.data();
}
};

#endif//GUARD_X_H 1
cat X.cc #include "X.h"

template<class T>
X<T>::X(const X& x): D(x.D) {
std::cerr << "copy constructor" << std::endl;
}

template<class T>
template<class S>
X<T>::X(const X<S>& x): D(x.data()) {
std::cerr << "conversion constructor" << std::endl;
}

template X<float>::X(con st X<int>& x);
template X<float>::X(con st X<float>& x);
g++ -Wall -ansi -pedantic -c X.cc

X.cc:15: error: ambiguous template specialization `X<>' \
for `X<float>::X(co nst X<float>&)'
X.cc:4: error: candidates are: \
X<T>::X(const X<T>&) [with T = float]
X.h:17: error: \
template<class S> X::X(const X<S>&) [with S = S, T = float]

What did I do wrong?
Jul 22 '05 #1
3 1080

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:ci******** **@nntp1.jpl.na sa.gov...
What did I do wrong?


Seems to compile without errors/warnings on Comeau C++ 4.3.3 and MSVC++ 7.1.

Regards,
Sumit.
--
Sumit Rajan <sumitrajan AT alexandria DOT cc>
Jul 22 '05 #2
Sumit Rajan wrote:
E. Robert Tisdale wrote:
What did I do wrong?


Seems to compile without errors/warnings on Comeau C++ 4.3.3 and MSVC++ 7.1.


I just tried it with my SGI MIPSpro C++ compiler
and it compiles without complaint there as well.

I guess that it's a GNU C++ compiler bug.

Can anybody suggest a work-around?
Jul 22 '05 #3
E. Robert Tisdale wrote:
What did I do wrong?


Hi,
it seems to be very old bug - look at
http://gcc.gnu.org/ml/gcc-prs/2000-q3/msg00288.html

To workaround it maybe try to use extra parameter: X<T>(X<S>, int).
The following code compiles well with gcc-3.4.

Yevgen
/////////////////////////////////////////////////
// X.h

#ifndef GUARD_X_H
#define GUARD_X_H 1
#include <iostream>

template<class T>
class X {
private:
// representation
T D;
public:
// functions
T data(void) const { return D; }
// constructors
X(T d = 0): D(d) { }
X(const X& x);
template<class S>
X(const X<S>& x, int i = 0);
friend
std::ostream& operator<<(std: :ostream& os, const X<T>& x) {
return os << x.data();
}
};

#endif//GUARD_X_H 1

///////////////////////////////////////////
// X.cc

#include "X.h"

template<class T>
X<T>::X(const X& x): D(x.D)
{
std::cerr << "copy constructor" << std::endl;
}

template<class T>
template<class S>
X<T>::X(const X<S>& x, int i): D(x.data())
{
std::cerr << "conversion constructor" << std::endl;
}
template X<float>::X(con st X<int>& x, int);
template X<float>::X(con st X<float>& x);

void f()
{
X<float> a(X<float>(1.0) );
X<float> b(X<int>(1));
}
Jul 22 '05 #4

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

Similar topics

4
2612
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in the code below. The function template < typename T >
4
1848
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
5
2241
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! Andy
6
2014
by: Thomas Maier-Komor | last post by:
Hi everybody, I am a little bit confused with the syntax of explicit instantiation, and I am not sure that it is possible to do what I want to do. Maybe someone has an idea. Consider a template class: template <class ABC> struct CT : public ABC
6
5678
by: Vyacheslav Lanovets | last post by:
Hello, All! I know that Explicit Instantiation actually emits code to obj files (so you can even export them from the module as plain functions or classes). But I found that MSVC7.1 compiler does the same in case of Explicit Specialization, so I either have to delcare specializations inline or move definitions to cpp file to avoid LNK2005 ("already defined") errors. Why is that?
8
4804
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; // this is not allowed: template <> struct B<int, float> {};
1
1157
by: Robin | last post by:
H My worthless textbook gives an example where you create a structure to mimic a Random Access file layout and you are expected to use an instantiation of that to pull data out of the Random Access file. Problem is of course that this is a value type variable and we need to have option explicit on so it won't box and unbox for me. I keep getting errors on the FileGet method Can somebody help as I am ready to throw the damn thing out the...
3
2444
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void some_func(T const& x) { }
3
4009
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
1
3077
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
0
9721
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
9600
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,...
0
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
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
9196
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...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4331
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
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.