delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/09/05:41:36

From: "Niklas Pettersson" <npedt97 AT student DOT vxu DOT se (nospam)>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: explicit instantiation
Date: Thu, 9 Mar 2000 10:08:52 +0100
Organization: Lund Institute of Technology, Sweden
Lines: 99
Message-ID: <8a7pnb$1pc$1@news.lth.se>
References: <62C1FE9FAA1E4584 DOT 06111F9979572068 DOT FEC73EFB7A77FD10 AT lp DOT airnews DOT net>
NNTP-Posting-Host: npedt97.univ.vxu.se
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hello!

I'm no expert on templates but I think I have found the error in your code.
First of all I'm curious to know why you want to make a explicit
instanciation, the C++ compiler will instanciate everything for you so I
can't see the meaning if doing it by your self(maybe for decreasing compile
time?). Anyway, I think the explicit instanciating need to be outside any
functions, and you must make a complete declaration of the function, that
includes the return type (void in your case). Here is some code that
works... One more thing.. Maybe you should consider using class instead of
typename. It's shorter and a more common way for C++ programmers to express
themselfs.

/ Niklas

#include <iostream>

using namespace std;

template <typename any>
void swap (any& x, any& y)
{
   any temp = x;

   x = y;
   y = temp;
}

template void swap<int>(int&, int&);

int main()
{
   int first  = 10;
   int second = 20;

   swap(first, second);

   cout << first << " " << second;

   return 0;
}






"Rodeo Red" <rodeored AT netstep DOT net> wrote in message
news:62C1FE9FAA1E4584 DOT 06111F9979572068 DOT FEC73EFB7A77FD10 AT lp DOT airnews DOT net...
> Does djgpp support explicit instatation of template functions ?
> This program produces these errors :
>
> C:\C++\C++PrimerPlus\basic.cpp: In function `int main()':
> C:\C++\C++PrimerPlus\basic.cpp:9: parse error before `template'
>
> I have no idea what the parse error could possibly be.
>
> My books say I can use :
> template  swap <int>(int, int);
> but none of the many books I have give even the slightest hint of a clue
> as to where it goes, so I have taken a wild guess. Perhaps it doesn't
> matter. I have tried it everywhere but it never works. What is the right
> place to put that line ? Why doesn't it work ?
>
>
>  #include <iostream>
>     using namespace std;
>
> template <typename any>
> void swap (any &, any &);//template for everything but int
>
>
> int main()
> {   template  swap <int>(int, int);//explicit instantiation for int
>     int first= 10;
>     int second=20;
>     swap(first, second);
>
>     return 0;
>  }
>
>
>     template <typename any>
>     void swap (any &x, any &y)
>     {   any temp;
>         temp = x;
>         x=y;
>         y=temp;
>
>     }
>
>
>
> Errors:
>
> C:\C++\C++PrimerPlus\swap.cpp: In function `int main()':
> C:\C++\C++PrimerPlus\swap.cpp:9: parse error before `template'


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019