Explicit specialization syntax

  • Thread starter Ney André de Mello Zunino
  • Start date
N

Ney André de Mello Zunino

Hello.

I'm trying to use that template specialization "trick" to allow one to
keep template implementations separate from the header file. However, I
am having trouble with the right syntax --I get a linker error when I
try to instantiate a Foo<int> on main(). Could anybody help?

// ======================== Foo.h ========================
#ifndef FOO_H__
#define FOO_H__

template<typename Type>
class Foo {
public:
Foo(Type object);
};

#endif
// =======================================================


// ======================= Foo.cpp =======================
#include "Foo.h"
#include <iostream>

template<typename Type>
Foo<Type>::Foo(Type object) {
std::cout << "Building Foo with object: " << object << "\n";
}

template<> Foo<int>::Foo(int);
// =======================================================


// ====================== Main.cpp =======================
#include "Foo.h"

int main() {
Foo<int> foo(10);
}
// =======================================================

Thank you,
 
M

Marcel Müller

Ney said:
// ======================= Foo.cpp =======================
#include "Foo.h"
#include <iostream>

template<typename Type>
Foo<Type>::Foo(Type object) {
std::cout << "Building Foo with object: " << object << "\n";
}

template<> Foo<int>::Foo(int);
// =======================================================

I don't know what you are going to achieve. But the above line
/declares/ a constructor of Foo<int>. There is no definition for it.

Did you mean

template<> Foo<int>::Foo(int)
{ std::cout << "Building Foo with object: " << object << "\n";
}


Marcel
 
V

Victor Bazarov

Hello.

I'm trying to use that template specialization "trick" to allow one to
keep template implementations separate from the header file. However, I
am having trouble with the right syntax --I get a linker error when I
try to instantiate a Foo<int> on main(). Could anybody help?

// ======================== Foo.h ========================
#ifndef FOO_H__
#define FOO_H__

template<typename Type>
class Foo {
public:
Foo(Type object);
};

#endif
// =======================================================


// ======================= Foo.cpp =======================
#include "Foo.h"
#include <iostream>

template<typename Type>
Foo<Type>::Foo(Type object) {
std::cout << "Building Foo with object: " << object << "\n";
}

template<> Foo<int>::Foo(int);

An explicit instantiation (not specialization) is what you're looking
for, I think. Try

template Foo said:
// =======================================================


// ====================== Main.cpp =======================
#include "Foo.h"

int main() {
Foo<int> foo(10);
}
// =======================================================

Thank you,

V
 
N

Ney André de Mello Zunino

Em 11/03/2011 16:08, Victor Bazarov escreveu:
An explicit instantiation (not specialization) is what you're looking
for, I think. Try

template Foo<int>::Foo(int);

Victor,

That did it. And you're right; I meant instantiation.

Now, for a follow-up question. Would it be possible to explicitly
instantiate the whole Foo class template instead of only its
constructor? What would the syntax look like?

Thanks again,
 
N

Ney André de Mello Zunino

Em 11/03/2011 16:38, Ney André de Mello Zunino escreveu:
Now, for a follow-up question. Would it be possible to explicitly
instantiate the whole Foo class template instead of only its
constructor? What would the syntax look like?

I have just tried the following explicit instantiation:

template class Foo<int>;

and it worked. I then tried adding another member function to the class
template, implemented it on the separate Foo.cpp file and tried invoking
it from main. No linker errors.

// ======= In Foo.h:

template<typename Type>
class Foo {
public:
Foo(Type object);
void bar(Type object) const; // new member function
};

// ======= In Foo.cpp:

template<typename Type>
void Foo<Type>::bar(Type object) const {
std::cout << "Foo<Type> invoked with object: " << object << "\n";
}

// ======= In Main.cpp:

int main() {
Foo<int> foo(10);
foo.bar(20);
}

P.S.: Sorry for I should have made those attempts before asking.
 
V

Victor Bazarov

Em 11/03/2011 16:38, Ney André de Mello Zunino escreveu:


I have just tried the following explicit instantiation:

template class Foo<int>;

and it worked. I then tried adding another member function to the class
template, implemented it on the separate Foo.cpp file and tried invoking
it from main. No linker errors.

// ======= In Foo.h:

template<typename Type>
class Foo {
public:
Foo(Type object);
void bar(Type object) const; // new member function
};

// ======= In Foo.cpp:

template<typename Type>
void Foo<Type>::bar(Type object) const {
std::cout << "Foo<Type> invoked with object: " << object << "\n";
}

I am guessing the c-tor and the explicit instantiation are here
somewhere, as well... :)
// ======= In Main.cpp:

int main() {
Foo<int> foo(10);
foo.bar(20);
}

P.S.: Sorry for I should have made those attempts before asking.

No harm done.

V
 
R

Richard

[Please do not mail me a copy of your followup]

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
<[email protected]> spake the secret code
Em 11/03/2011 16:08, Victor Bazarov escreveu:


Victor,

That did it. And you're right; I meant instantiation.

This is also covered in the FAQ. There's lots of really useful
information there, I would recommend familiarizing yourself with the
entire FAQ on at least a skimming level.

[35.13] How can I avoid linker errors with my template functions?
<http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13>
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top