🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1852

Re: Template instantiation question

#1852
From: Fergus Henderson
Date: Sun, 27 Aug 1995 19:10
54 lines
2797 bytes
clamage@Eng (Steve Clamage) writes:

>tony@online.tmx.com.au (Tony Cook) writes:
>
>>Steve Clamage (clamage@Eng) wrote:
>
>>: In the mean time, you could write a specialization of Foo<X>::compare
>>: that throws an exception or invokes "assert(0)" to allow your code
>>: to compile and link. That is, for example, if you use Foo<complex>,
>>: you could provide the specialization
>>: 	int Foo<complex>::compare(const complex&) { assert(0); return 0; }
>>: which will prevent the compiler from generating a version from the template,
>>: and will thus allow your program to compile and link.
>
>>Can we instead _declare_ the specialization and not define it:
>
>>    extern int Foo<complex>::compare(const complex &);
>
>>So that we see an error at link time rather than run-time?
>
>No. The declaration does not prevent instantiation if any use is made
>of the function. That is, the declaration tells the compiler that
>particular compare function is supposed to exist. If it is needed
>but not found at link time, the system will generate it from the
>template definition.

However, if you're really keen on detecting the error at link time
rather than at runtime, it is possible to do so.
You can put the definition of Foo<complex>::compare() in a compilation
unit of its own, put that compilation unit in a library, and make sure
that the definition references some other bogus function which is
declared but not defined.

    extern int Foo<complex>::compare(const complex &) {
	extern void this_is_totally_bogus();	// declared but not defined
	this_is_totally_bogus();
	return 0;
    }

That way, if Foo<complex>::compare is referenced, the above compilation
unit will be linked in, and hence you will get an undefined reference
to `this_is_totally_bogus()'.

All things considered, however, I'm not really sure that it's worth the effort.

-- 
Fergus Henderson             |  #define x t=a[i],a[i]=a[m],a[m]=t
                             |  char a[]=" 12345678";main(m,i,j,t){for(i=m;i<
fjh@cs.mu.oz.au              |  9;x,i++)for(x,j=m;--j?(t=a[m-j]-a[m])-j&&t+j:
http://www.cs.mu.oz.au/~fjh  |  main(m+1)*0;);m-9||puts(a+1);} /* 8 queens */
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <fjh-950828-051017@cs.mu.oz.au>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <41b09t$ltj@engnews2.Eng.Sun.COM> <199508220325.NAA11355@online.tmx.com.au> <41cvdk$4bp@engnews2.Eng.Sun.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 27 Aug 95 19:10:17 GMT
From: fjh@cs.mu.oz.au (Fergus Henderson)
Approved: fjh@cs.mu.oz.au
Message-ID: <fjh-950828-051017@cs.mu.oz.au>
Newsgroups: comp.std.c++
Subject: Re: Template instantiation question
References: <41b09t$ltj@engnews2.Eng.Sun.COM> <199508220325.NAA11355@online.tmx.com.au> <41cvdk$4bp@engnews2.Eng.Sun.COM>