Topic: const reference argument deduction for binding a temporary
Author: rogero <roger.orr@gmail.com>
Date: Tue, 6 Jul 2010 02:28:54 CST Raw View
As I understand it the following code:
template <typename T>
void v(T &)
{}
class C {};
int main()
{
v(C());
}
fails to compile as template argument deduction deduces T = C and then
fails to bind the temporary to C&.
If I'm right (FWIW Comeau fails this code) should/could the template
deduction rule for references handle temporaries differently and
deduce T as "C const" ?
Regards,
Roger Orr
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Florian <flob90@wanadoo.fr>
Date: Sat, 10 Jul 2010 23:37:38 CST Raw View
I don't understand your question, this code fails because you can take
a reference from a temporary. The type deduces by template is C not C
const.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Paul Bibbings <paul.bibbings@gmail.com>
Date: Mon, 12 Jul 2010 11:58:10 CST Raw View
rogero <roger.orr@gmail.com> writes:
> As I understand it the following code:
>
> template <typename T>
> void v(T &)
> {}
>
> class C {};
>
> int main()
> {
> v(C());
> }
>
> fails to compile as template argument deduction deduces T = C and then
> fails to bind the temporary to C&.
> If I'm right (FWIW Comeau fails this code) should/could the template
> deduction rule for references handle temporaries differently and
> deduce T as "C const" ?
If this were permitted, then wouldn't the following extended example
result in an unwanted ambiguity for call #2?
template<typename T>
void v(T&) { }
template<typename T>
void v(const T&) { }
class C { };
int main()
{
C c;
v(c); // #1: calls v<C>(C&)
v(C()); // #2: calls v<C>(C const&)
}
Regards
Paul Bibbings
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: SG <s.gesemann@gmail.com>
Date: Mon, 12 Jul 2010 11:58:27 CST Raw View
On 6 Jul., 10:28, rogero wrote:
>
> As I understand it the following code:
>
> template <typename T>
> void v(T &);
>
> class C {};
>
> int main() {
> v(C());
> }
>
> fails to compile as template argument deduction deduces T = C and
> then fails to bind the temporary to C&.
Yes.
> If I'm right (FWIW Comeau fails this code) should/could the template
> deduction rule for references handle temporaries differently and
> deduce T as "C const" ?
This example is not exactly a strong case for such a change. Why don't
you simply write
template <typename T>
void v(T const &);
or
template <typename T>
void v(T &&);
?
Note: To fully understand what the last version does you need to look
at
(*) the deduction rules for T&&
(*) reference collapsing
(*) reference binding rules
Cheers!
SG
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Mon, 12 Jul 2010 23:57:43 CST Raw View
Paul Bibbings wrote:
>
> rogero <roger.orr@gmail.com> writes:
>
>> As I understand it the following code:
>>
>> template <typename T>
>> void v(T &)
>> {}
>>
>> class C {};
>>
>> int main()
>> {
>> v(C());
>> }
>>
>> fails to compile as template argument deduction deduces T = C and then
>> fails to bind the temporary to C&.
>> If I'm right (FWIW Comeau fails this code) should/could the template
>> deduction rule for references handle temporaries differently and
>> deduce T as "C const" ?
>
> If this were permitted, then wouldn't the following extended example
> result in an unwanted ambiguity for call #2?
I do not think so. It would be easy ro write and apply a
disambiguation rule; just prefer the exact match to the one requiring
modification of the parameter.
>
> template<typename T>
> void v(T&) { }
>
> template<typename T>
> void v(const T&) { }
>
> class C { };
>
> int main()
> {
> C c;
> v(c); // #1: calls v<C>(C&)
> v(C()); // #2: calls v<C>(C const&)
> }
>
> Regards
>
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Mon, 12 Jul 2010 23:57:42 CST Raw View
Florian wrote:
> I don't understand your question, this code fails because you can take
> a reference from a temporary. The type deduces by template is C not C
> const.
>
> You are right, you do not understand the question. Roger certainly knows
what the current rules are but he is enquiring as to whether those rules
could be modified so that when the argument is a temporary the compiler adds
a const into the parameter.
I think the answer should be no. This is (IMHO) a code quality issue. If the
writer of the template wanted it to be a const ref parameter he could have
made it so, that he didn't suggests that (if the coder is competent) that
there is a good reason not to do so.
Now is there a motivating example for change. I.e. is there a case where it
is not just a matter of code quality?
Well roger?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: rogero <roger.orr@gmail.com>
Date: Wed, 14 Jul 2010 03:22:59 CST Raw View
On Jul 13, 6:57 am, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:
> Now is there a motivating example for change. I.e. is there a case where it
> is not just a matter of code quality?
The motivating example was using a template function to implement a
visitor pattern.
Some visitors are modified, others are read-only - these have the
corresponding method 'const'
By default Microsoft VC allows binding of temporaries to rvalues, so
passing a temporary object with a const method worked. [However, it
does give a warning]
Other compilers won't compile the code.
There are three obvious solutions.
(a) An overload taking const &
[duplicates the source code]
(b) Use a temporary const & variable
[slightly messy]
(c) Explicitly providing the template argument
[error prone]
It just seemed a shame that the compiler picked a template
instantiation using the non-const T and then rejects it as invalid for
a temporary.
Roger.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Wed, 14 Jul 2010 11:36:41 CST Raw View
On Jul 14, 11:22 am, rogero <roger....@gmail.com> wrote:
> There are three obvious solutions.
> (a) An overload taking const &
> [duplicates the source code]
> (b) Use a temporary const & variable
> [slightly messy]
> (c) Explicitly providing the template argument
> [error prone]
>
> It just seemed a shame that the compiler picked a template
> instantiation using the non-const T and then rejects it as invalid for
> a temporary.
A function template is supposed to deduce the actual arguments
in your example. This deduction *could* succeed, if the
temporary where a temporary of const type. Alas it is not
in your example, so here the diagnostic is the outcome
of that. I don't see any reason for the compiler or the standard
to be ashamed of that.
In your particular situation the perfectly forwarding signature
of C++0x would be a perfect candidate for your solution,
because neither of your bullets (a)-(c) are needed.
HTH & Greetings from Bremen,
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++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: SG <s.gesemann@gmail.com>
Date: Wed, 14 Jul 2010 11:32:59 CST Raw View
On 14 Jul., 11:22, rogero wrote:
>
> There are three obvious solutions.
> (a) An overload taking const &
> [duplicates the source code]
> (b) Use a temporary const & variable
> [slightly messy]
> (c) Explicitly providing the template argument
> [error prone]
>
> It just seemed a shame that the compiler picked a template
> instantiation using the non-const T and then rejects it as invalid for
> a temporary.
I still don't exactly know what it is you're trying to do. As far as
I can tell there's nothing shameful about the deduction rules. It
looks like what you're trying to do is similar to "perfect
forwarding". If that's the case we don't need to change the deduction
rules like you suggested but rather use an rvalue reference and rely
on the rvalue reference deduction rules and reference collapsing.
Cheers!
SG
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Wed, 14 Jul 2010 11:32:58 CST Raw View
On Jul 14, 5:22 am, rogero <roger....@gmail.com> wrote:
> On Jul 13, 6:57 am, Francis Glassborow
>
> <francis.glassbo...@btinternet.com> wrote:
> > Now is there a motivating example for change. I.e. is there a case where
it
> > is not just a matter of code quality?
>
> The motivating example was using a template function to implement a
> visitor pattern.
>
> Some visitors are modified, others are read-only - these have the
> corresponding method 'const'
>
> By default Microsoft VC allows binding of temporaries to rvalues, so
> passing a temporary object with a const method worked. [However, it
> does give a warning]
> Other compilers won't compile the code.
>
> There are three obvious solutions.
> (a) An overload taking const &
> [duplicates the source code]
> (b) Use a temporary const & variable
> [slightly messy]
> (c) Explicitly providing the template argument
> [error prone]
>
> It just seemed a shame that the compiler picked a template
> instantiation using the non-const T and then rejects it as invalid for
> a temporary.
In C++0X you'll be able to say:
template <typename T>
void v(T&&)
{}
class C {};
int main()
{
v(C()); // ok
}
More info at:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html
This has already been implemented in several recent compilers.
-Howard
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]