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

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(const X<int>& x);
template X<float>::X(const X<float>& x);
g++ -Wall -ansi -pedantic -c X.cc

X.cc:15: error: ambiguous template specialization `X<>' \
for `X<float>::X(const 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 1064

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:ci**********@nntp1.jpl.nasa.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(const X<int>& x, int);
template X<float>::X(const 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
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...
4
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...
5
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! ...
6
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...
6
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...
8
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 {}; ...
1
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...
3
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...
3
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...
1
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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
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...

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.