Topic: Has C++ got room for a more flexible function template argument
Author: dsp@bdal.de (=?ISO-8859-1?Q?=22Daniel_Kr=FCgler_=28ne_Spangenberg=29=22?=)
Date: Mon, 7 Jun 2004 16:47:04 +0000 (UTC) Raw View
Hello, Rob Williscroft,
Rob Williscroft schrieb:
[snip]
>#include <deque>
>#include <string>
>
>template < typename T >
>struct dont_deduce
>{
> typedef T type;
>};
>
>template< class T, class A >
>void insert( std::deque<T,A>& c, typename dont_deduce<T>::type v )
>{
> c.push_back( v );
>}
>
>int main()
>{
> std::deque< std::string > ds;
> std::deque<float> df;
> insert( ds, "foo" );=20
> insert( df, 1 );
>}
>
>Once we get template typedefs this might become:
>
>template< class T, class A >
>void insert( std::deque<T,A>& c, std::identity<T> v )
>{
> c.push_back( v );
>}
>
>This leaves what happens in the programmers hands:
>
>template < typename T >
>struct dont_deduce
>{
> typedef T type;
>};
>
>
>template < typename T >
>struct thing
>{
> thing() {}
> thing( int ) {}
>};
>
>template< class T >
>void whatever( thing< typename dont_deduce<T>::type > const& c, T v )
>{
>}
>
>int main()
>{
> whatever( 0, 1.2f );
>
> thing< int > ti;
> whatever( ti, 0 );
>}
>
>I can't say I've ever found a use for it, but I'm sure somebody has.
>
I have found one! Consider the following problem: In a simplified=20
scenario I wanted to apply a
general sorting function and wrote in a first attempt:
template <typename RanIt>
inline void DoSort(RanIt it, RanIt e, void (*srt)(RanIt, RanIt))
{
srt(it, e);
}
and tried invokation of std::stable_sort<> via:
std::string s;
DoSort(s.begin(), s.end(), &std::stable_sort);
Now the corresponding function template instantiation failes, because=20
although the 3rd argument srt
of DoSort seems to be determined by the other arguments, due to the=20
deduction rules of 14.8.2.4
(as nicely pointed out by Vladim=EDr Marko) the 3rd argument **must**=20
participate in template argument
deduction and functions templates are not allowed as such thingies.=20
After applying your technique I
ended with
template <typename RanIt>
inline void DoSort(RanIt it, RanIt e, typename dont_deduce<void=20
(*)(RanIt, RanIt)>::type srt)
{
srt(it, e);
}
and everything was fine!
Thank you very much for your inspiration,
Daniel Kr=FCgler
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: llewelly.at@xmission.dot.com (llewelly)
Date: Tue, 8 Jun 2004 19:06:37 +0000 (UTC) Raw View
nesotto@cs.auc.dk ("Thorsten Ottosen") writes:
> "Rob Williscroft" <rtw@freenet.co.uk> wrote in message news:Xns95023575022EFukcoREMOVEfreenetrtw@130.133.1.4...
> | "Thorsten Ottosen" wrote in
> | news:40c17f35$0$8989$afc38c87@news.optusnet.com.au in comp.std.c++:
> |
> | >| template< class T, class A >
> | >| void insert( std::deque<T,A>& c, typename dont_deduce<T>::type v )
> | >| {
> | >| c.push_back( v );
> | >| }
> | >
> | > nice. but it don't "solve" anything (if there is anything to solve).
> |
> | ???
>
> you still have to know about dont_deduce<> or the hack of using an extra template argument.
> That's the "problem".
>
> |And even if we gain a warning, we also lose an error:
> |
> |#include <algorithm>
> |
> |int main()
> |{
> | char buf[10];
> |
> | std::copy( "123456789", 0, buf );
> |}
> |
>
> Who uses arrays anyway?
[snip]
Everyone? Have you ever worked on a large C++ project which *doesn't*
use arrays? I haven't.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (Seungbeom Kim)
Date: Tue, 8 Jun 2004 21:01:06 +0000 (UTC) Raw View
Rob Williscroft wrote:
>
> And even if we gain a warning, we also lose an error:
>
> #include <algorithm>
>
> int main()
> {
> char buf[10];
>
> std::copy( "123456789", 0, buf );
> }
By the way, what does this program do (assuming we change the 0
to static_cast<const char*>(0) to make it compile)?
Is a null pointer a valid iterator input to std::copy? I doubt it.
--
Seungbeom Kim
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tom_usenet@hotmail.com (tom_usenet)
Date: Fri, 4 Jun 2004 16:09:11 +0000 (UTC) Raw View
On Thu, 3 Jun 2004 17:25:58 +0000 (UTC), nesotto@cs.auc.dk ("Thorsten
Ottosen") wrote:
>Condsider this function
>
>template< class T, class A >
>void insert( deque<T,A>& c, T v )
>{
> c.push_back( v );
>}
>
>deque<string> ds;
>deque<float> df;
>insert( ds, "foo" ); // error
>insert( df, 1 ); // error
>
>If we change the definition of insert to
>
>template< class T, class A, class U >
>void insert( deque<T,A>& c, U v )
>{
> c.push_back( v );
>}
>
>insert( ds, "foo" ); // no error
>insert( df, 1 ); // no error
>
>One might say it is a little inconvenient to have to specify the third template argument.
>Can anyone deduce if it would be feasable to allow the original version to compile
>given that there are no other overloads of the form void insert( deque<T,A>&, U ) ?
It seems feasible, though a bit complicated for compiler writers. You
just need to change the rules about when the deduced P can differ from
A, and what to do if a unique value of T isn't found.
If you don't deduce a unique value for a template parameter, but
multiple different values, then the compiler could try to substitute
the different values into all of the parameters. If only one of the
deduced values were valid for the arguments (i.e. there is a
conversion from the argument type to the synthesized parameter type),
it could go with that one, otherwise it would be ambiguous.
In the case where you get multiple deduced values for multiple
parameters, every combination could be tried, and if a unique viable
signature is found, that could be used, otherwise it would be
ambiguous.
Because this only affects TAD, it wouldn't be too wide reaching a
change. It would break existing code though, causing different
overloads to be chosen to those that are currently chosen, although
this would be rare I think, and indicative of overly fragile code.
I'm sure there are other issues I haven't thought of though.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tom_usenet@hotmail.com (tom_usenet)
Date: Fri, 4 Jun 2004 17:43:02 +0000 (UTC) Raw View
On Fri, 4 Jun 2004 16:09:11 +0000 (UTC), tom_usenet@hotmail.com
(tom_usenet) wrote:
>If you don't deduce a unique value for a template parameter, but
>multiple different values, then the compiler could try to substitute
>the different values into all of the parameters. If only one of the
>deduced values were valid for the arguments (i.e. there is a
>conversion from the argument type to the synthesized parameter type),
>it could go with that one, otherwise it would be ambiguous.
By "otherwise it would be ambiguous", I really mean "otherwise
template argument deduction would fail". (same in the next paragraph)
I might be able to put together a proposal, but I haven't got the
money to turn up to standards meetings. And comment would be required
from a compiler writer or TAD guru before going any further.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]