Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15118

Re: Any way to defer template function overloading?

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Any way to defer template function overloading?
Date 2012-04-01 16:06 +1200
Message-ID <9tq2ijFm3gU1@mid.individual.net> (permalink)
References <jl8d4h$hbf$1@speranza.aioe.org>

Show all headers | View raw


On 04/ 1/12 02:05 PM, Qi wrote:
> Let me show some test code.
>
> =====code start
>
> template<typename T>
> void abc(const T&)
> {
> 	cout<<  "1"<<  endl;
> }
>
> template<typename T>
> void test(const T&  a)
> {
> 	abc(a);
> }
>
> void abc(const string&)
> {
> 	cout<<  "2"<<  endl;
> }
>
> int main(int argc, char * argv[])
> {
> 	(void)argc;
> 	(void)argv;

Why do this?

> 	string s;
> 	test(s);
> 	
> 	return 0;
> }
>
> =====code end
>
> "abc" is a template function that I use as some kind of traits.
> (similar as get_pointer in Boost).
> The "string" overloading is after the "test" function.
>
> "test" is the function where using "abc". "test" is a template
> function too.
>
> "main" is the function that using "test". It's guaranteed that
> all overloading will be declared before main (but may after "test").
>
> In standard way, above code will print "1" on GCC (VC will print "2"
> because of its less standard template parsing).
>
> My question is, is there any way to make "test" chooses
> the "string" overloading and print 2, no matter the overloading
> is before or after "test"?

Make the function a specialisation:

template <>
void abc(const std::string&)
{
   std::cout << "2" << std::endl;
}

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Any way to defer template function overloading? Qi <no@no.no> - 2012-04-01 10:05 +0800
  Re: Any way to defer template function overloading? Ian Collins <ian-news@hotmail.com> - 2012-04-01 16:06 +1200
    Re: Any way to defer template function overloading? Qi <no@no.no> - 2012-04-01 13:53 +0800
  Re: Any way to defer template function overloading? Juha Nieminen <nospam@thanks.invalid> - 2012-04-01 17:24 +0000
    Re: Any way to defer template function overloading? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-04-02 00:40 +0200
      Re: Any way to defer template function overloading? Ian Collins <ian-news@hotmail.com> - 2012-04-02 11:02 +1200
      Re: Any way to defer template function overloading? Juha Nieminen <nospam@thanks.invalid> - 2012-04-02 05:29 +0000
    Re: Any way to defer template function overloading? Qi <no@no.com> - 2012-04-02 13:48 +0800
      Re: Any way to defer template function overloading? Ian Collins <ian-news@hotmail.com> - 2012-04-02 17:53 +1200
        Re: Any way to defer template function overloading? Qi <no@no.com> - 2012-04-02 13:57 +0800
          Re: Any way to defer template function overloading? Ian Collins <ian-news@hotmail.com> - 2012-04-02 18:12 +1200
            Re: Any way to defer template function overloading? Miles Bader <miles@gnu.org> - 2012-04-02 15:32 +0900
      Re: Any way to defer template function overloading? Miles Bader <miles@gnu.org> - 2012-04-02 14:58 +0900
      Re: Any way to defer template function overloading? Juha Nieminen <nospam@thanks.invalid> - 2012-04-02 10:11 +0000
        Re: Any way to defer template function overloading? Qi <no@no.com> - 2012-04-02 19:04 +0800
          Re: Any way to defer template function overloading? Qi <no@no.com> - 2012-04-02 19:17 +0800
  Re: Any way to defer template function overloading? jgk@panix.com (Joe keane) - 2012-04-02 14:41 +0000

csiph-web