Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicit specialization syntax

Reply
Thread Tools

Explicit specialization syntax

 
 
Ney André de Mello Zunino
Guest
Posts: n/a
 
      03-11-2011
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,

--
Ney André de Mello Zunino
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      03-11-2011
Ney André de Mello Zunino wrote:
> // ======================= 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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-11-2011
On 3/11/2011 1:08 PM, Ney André de Mello Zunino wrote:
> 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<int>::Foo(int);

> // ================================================== =====
>
>
> // ====================== Main.cpp =======================
> #include "Foo.h"
>
> int main() {
> Foo<int> foo(10);
> }
> // ================================================== =====
>
> Thank you,
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Ney André de Mello Zunino
Guest
Posts: n/a
 
      03-11-2011
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,

--
Ney André de Mello Zunino
 
Reply With Quote
 
Ney André de Mello Zunino
Guest
Posts: n/a
 
      03-11-2011
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.

--
Ney André de Mello Zunino
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-11-2011
On 3/11/2011 3:01 PM, Ney André de Mello Zunino wrote:
> 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";
> }


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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      03-11-2011
[Please do not mail me a copy of your followup]

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
<(E-Mail Removed)> spake the secret code
<ildtn9$lmf$(E-Mail Removed)> thusly:

>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.


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>
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Explicit and partial template specialization nested in classes. Patrick Kowalzick C++ 0 10-29-2004 02:45 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM
Nested templates explicit specialization Andriy Shnyr C++ 3 12-04-2003 04:54 AM



Advertisments