Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Preventing multiple instantiations of a templated function

Reply
Thread Tools

Preventing multiple instantiations of a templated function

 
 
Old Wolf
Guest
Posts: n/a
 
      01-21-2007
Dave Rahardja wrote:
> Hi all,
>
> Is it possible to prevent _at compile or link time_ the mulitple instantiation
> of a templated function? In other words, if there exists a function
>
> template <typename T> void fn();
>
> I want to prevent the user from doing this:
>
> int main()
> {
> fn<int>();
> fn<double>(); // <-- should cause compile time or link time error
> }


You could set up a script to parse the linker output and see if
the function has been instantiated twice. This is system-specific
of course

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      01-22-2007
Old Wolf wrote:
> Dave Rahardja wrote:
> > Hi all,
> >
> > Is it possible to prevent _at compile or link time_ the mulitple instantiation
> > of a templated function? In other words, if there exists a function
> >
> > template <typename T> void fn();
> >
> > I want to prevent the user from doing this:
> >
> > int main()
> > {
> > fn<int>();
> > fn<double>(); // <-- should cause compile time or link time error
> > }

>
> You could set up a script to parse the linker output and see if
> the function has been instantiated twice. This is system-specific
> of course


It might also be possible to schedule twice-monthly, on-site code
inspections of each client installation that uses this function
template - as yet another way to provide an extra margin of safety.
After all, it's hard to top in situ verfication by the very same
engineer or engineers who wrote the library - to determine for sure
whether the client has properly met all of its requirements. Effective?
surely, but practical? not hardly.

In the real world, a well-designed software interface should enforce
its own requirements (and report - with at least a semi-coherent
explanation - when any of its requirements have not been met). A
poorly-designed software interface, in constrast, is often one designed
to suit the convenience and tastes of its implementor. As such, this
kind of interface usually requires the client to absorb a set of
"just-so" knowledge describing the proper use of the software - a set
of requirements that often seem to follow no known existing convention
or practice, be motivated by no discernable rationale or purpose, yet
nonetheless will fail - even silently - unless its inscrutable
requirements and procedures are followed to a tee.

So, if at anytime, it starts to look that a particular software
interface under design is looking more and more like it belongs in the
latter category instead of the former, then a top-design review of the
entire interface is probably in order.

Greg

 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      01-22-2007
>> I don't understand how your solution works. Could you elaborate on what
>> SameType<...> does?

>
>Checks whether the types are the same. I thought that would be clear.
>But then, I'm not the world's greatest communicator...
>
> template< typename T, typename U >
> struct SameType { enum { yes = false }; };
>
> template< typename T >
> struct SameType<T, T> { enum { yes = true }; }
>
>That leaves COMPILE_TIME_ASSERT and where ::FnType comes from.
>
>The Boost library has a nice COMPILE_TIME_ASSERT.
>
>::FnType must be defined by the client code.
>
>Since it's at namespace scope (I used the global namespace for purpose
>of illustration) it can't be duplicated if it's a class. And to make
>more sure that it's a class you can let the function refer to a nested
>typedef. The client code's definition might then look like
>
> struct FnType { typedef double Type; };
>
>and the function definition as
>
> template< typename T >
> void fn()
> {
> COMPILE_TIME_ASSERT( SameType<T, ::FnType::Type>::yes );
> // ...
> }
>
>OK? Well, disclaimer: I haven't tried this. But I can't think why it
>shouldn't work.


I see. However this pushes the responsibility for creating the check type
(FnType) to the user of the library, where she has to ensure that every
instantiation of fn() will see the _same_ FnType. Putting something in the
global namespace is also undesirable for my library design. I've been burned
too many times by symbol clashes when two libraries collide.

-dr
 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      01-22-2007
On 21 Jan 2007 14:47:58 -0800, "Old Wolf" <(E-Mail Removed)> wrote:

>Dave Rahardja wrote:
>> Hi all,
>>
>> Is it possible to prevent _at compile or link time_ the mulitple instantiation
>> of a templated function? In other words, if there exists a function
>>
>> template <typename T> void fn();
>>
>> I want to prevent the user from doing this:
>>
>> int main()
>> {
>> fn<int>();
>> fn<double>(); // <-- should cause compile time or link time error
>> }

>
>You could set up a script to parse the linker output and see if
>the function has been instantiated twice. This is system-specific
>of course


This is the very situation that I'm trying to prevent: a manual,
build-specific, or team process-based solution. I'm building a generic library
that must work on a wide variety of compilers and target microprocessors, and
I want semantic checks to be as automatic as possible, using standard C++ as
much as possible.

-dr
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-22-2007
* Dave Rahardja:
>>> I don't understand how your solution works. Could you elaborate on what
>>> SameType<...> does?

>> Checks whether the types are the same. I thought that would be clear.
>> But then, I'm not the world's greatest communicator...
>>
>> template< typename T, typename U >
>> struct SameType { enum { yes = false }; };
>>
>> template< typename T >
>> struct SameType<T, T> { enum { yes = true }; }
>>
>> That leaves COMPILE_TIME_ASSERT and where ::FnType comes from.
>>
>> The Boost library has a nice COMPILE_TIME_ASSERT.
>>
>> ::FnType must be defined by the client code.
>>
>> Since it's at namespace scope (I used the global namespace for purpose
>> of illustration) it can't be duplicated if it's a class. And to make
>> more sure that it's a class you can let the function refer to a nested
>> typedef. The client code's definition might then look like
>>
>> struct FnType { typedef double Type; };
>>
>> and the function definition as
>>
>> template< typename T >
>> void fn()
>> {
>> COMPILE_TIME_ASSERT( SameType<T, ::FnType::Type>::yes );
>> // ...
>> }
>>
>> OK? Well, disclaimer: I haven't tried this. But I can't think why it
>> shouldn't work.

>
> I see. However this pushes the responsibility for creating the check type
> (FnType) to the user of the library,


Yes of course. It's the user that uses the template and decides the
template argument, no? Only the user knows that type.


> where she has to ensure that every
> instantiation of fn() will see the _same_ FnType.


Yes, that's ensured by the one-definition-rule (ODR), paragraph
something of the standard.


> Putting something in the
> global namespace is also undesirable for my library design. I've been burned
> too many times by symbol clashes when two libraries collide.


The use a namespace specific to your library. In C++ namespaces can be
extended.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
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
Exposing certain instantiations of a templated function Bogdan C++ 1 12-04-2010 03:51 PM
Defining templated member function outside templated class chhenning C++ 5 02-13-2008 07:36 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments