Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7945 > unrolled thread
| Started by | Noah Roberts <dont@email.me> |
|---|---|
| First post | 2011-07-18 15:46 -0700 |
| Last post | 2011-07-20 12:53 -0700 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.c++
static_cast, cast operator, and constructors. Noah Roberts <dont@email.me> - 2011-07-18 15:46 -0700
Re: static_cast, cast operator, and constructors. Ian Collins <ian-news@hotmail.com> - 2011-07-19 11:29 +1200
Re: static_cast, cast operator, and constructors. Noah Roberts <dont@email.me> - 2011-07-18 16:44 -0700
Re: static_cast, cast operator, and constructors. Ian Collins <ian-news@hotmail.com> - 2011-07-19 11:57 +1200
Re: static_cast, cast operator, and constructors. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-07-19 13:12 +0000
Re: static_cast, cast operator, and constructors. Noah Roberts <dont@email.me> - 2011-07-19 09:26 -0700
Re: static_cast, cast operator, and constructors. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-07-20 18:52 +0000
Re: static_cast, cast operator, and constructors. James Kanze <james.kanze@gmail.com> - 2011-07-20 12:53 -0700
| From | Noah Roberts <dont@email.me> |
|---|---|
| Date | 2011-07-18 15:46 -0700 |
| Subject | static_cast, cast operator, and constructors. |
| Message-ID | <4e24b4fd$0$11125$cc2e38e6@news.uslec.net> |
Considder:
struct ObjectA
{
ObjectA(ObjectA const&);
template < typename T >
ObjectA(T const&);
void fun() const;
};
struct ObjectB
{
operator ObjectA () const;
};
static_cast<ObjectA>(an_object_b).fun();
Which constructor does the standard specify will be called during the
static_cast: copy or template?
Behavior I am seeing is that the templated version is called and then
bases are called on the result of the cast operator.
Is there a way to explicitly call the cast operator or is that what I'm
supposedly doing?
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-19 11:29 +1200 |
| Message-ID | <98ju0cFl78U3@mid.individual.net> |
| In reply to | #7945 |
On 07/19/11 10:46 AM, Noah Roberts wrote:
> Considder:
>
> struct ObjectA
> {
> ObjectA(ObjectA const&);
>
> template< typename T>
> ObjectA(T const&);
>
> void fun() const;
> };
>
> struct ObjectB
> {
> operator ObjectA () const;
> };
>
> static_cast<ObjectA>(an_object_b).fun();
>
> Which constructor does the standard specify will be called during the
> static_cast: copy or template?
Is the code complete? The only option is the template.
> Behavior I am seeing is that the templated version is called and then
> bases are called on the result of the cast operator.
>
> Is there a way to explicitly call the cast operator or is that what I'm
> supposedly doing?
Specialise the template constructor for ObjectB.
template <>
ObjectA::ObjectA<ObjectB>(ObjectB const&)
{
std::cout << "special" << std::endl;
}
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Noah Roberts <dont@email.me> |
|---|---|
| Date | 2011-07-18 16:44 -0700 |
| Message-ID | <4e24c29d$0$11121$cc2e38e6@news.uslec.net> |
| In reply to | #7946 |
On 7/18/2011 4:29 PM, Ian Collins wrote:
> On 07/19/11 10:46 AM, Noah Roberts wrote:
>> Considder:
>>
>> struct ObjectA
>> {
>> ObjectA(ObjectA const&);
>>
>> template< typename T>
>> ObjectA(T const&);
>>
>> void fun() const;
>> };
>>
>> struct ObjectB
>> {
>> operator ObjectA () const;
>> };
>>
>> static_cast<ObjectA>(an_object_b).fun();
>>
>> Which constructor does the standard specify will be called during the
>> static_cast: copy or template?
>
> Is the code complete? The only option is the template.
No, there's a second option: apply the cast operator and call the copy
operator with the result.
>
>> Behavior I am seeing is that the templated version is called and then
>> bases are called on the result of the cast operator.
>>
>> Is there a way to explicitly call the cast operator or is that what I'm
>> supposedly doing?
>
> Specialise the template constructor for ObjectB.
>
> template <>
> ObjectA::ObjectA<ObjectB>(ObjectB const&)
> {
> std::cout << "special" << std::endl;
> }
>
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-19 11:57 +1200 |
| Message-ID | <98jvlqF8muU2@mid.individual.net> |
| In reply to | #7947 |
On 07/19/11 11:44 AM, Noah Roberts wrote:
> On 7/18/2011 4:29 PM, Ian Collins wrote:
>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>> Considder:
>>>
>>> struct ObjectA
>>> {
>>> ObjectA(ObjectA const&);
>>>
>>> template< typename T>
>>> ObjectA(T const&);
>>>
>>> void fun() const;
>>> };
>>>
>>> struct ObjectB
>>> {
>>> operator ObjectA () const;
>>> };
>>>
>>> static_cast<ObjectA>(an_object_b).fun();
>>>
>>> Which constructor does the standard specify will be called during the
>>> static_cast: copy or template?
>>
>> Is the code complete? The only option is the template.
>
> No, there's a second option: apply the cast operator and call the copy
> operator with the result.
Your terminology is a little confused. There is a conversion (not cast)
operator and a copy constructor (not operator).
The sequence you describe required two levels of conversion, the
template constructor one.
Assignment or omitting the template constructor should call the
conversion operator:
ObjectA a = an_object_b;
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-07-19 13:12 +0000 |
| Message-ID | <slrnj2b0l5.gtj.grahn+nntp@frailea.sa.invalid> |
| In reply to | #7949 |
On Mon, 2011-07-18, Ian Collins wrote:
> On 07/19/11 11:44 AM, Noah Roberts wrote:
>> On 7/18/2011 4:29 PM, Ian Collins wrote:
>>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>>> Considder:
>>>>
>>>> struct ObjectA
>>>> {
...
>> No, there's a second option: apply the cast operator and call the copy
>> operator with the result.
>
> Your terminology is a little confused. There is a conversion (not cast)
> operator and a copy constructor (not operator).
And I personally get confused by a class called "ObjectA". It's a
class, not an object.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | Noah Roberts <dont@email.me> |
|---|---|
| Date | 2011-07-19 09:26 -0700 |
| Message-ID | <4e25ad6c$0$11123$cc2e38e6@news.uslec.net> |
| In reply to | #7967 |
On 7/19/2011 6:12 AM, Jorgen Grahn wrote:
> On Mon, 2011-07-18, Ian Collins wrote:
>> On 07/19/11 11:44 AM, Noah Roberts wrote:
>>> On 7/18/2011 4:29 PM, Ian Collins wrote:
>>>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>>>> Considder:
>>>>>
>>>>> struct ObjectA
>>>>> {
> ...
>
>>> No, there's a second option: apply the cast operator and call the copy
>>> operator with the result.
>>
>> Your terminology is a little confused. There is a conversion (not cast)
>> operator and a copy constructor (not operator).
>
> And I personally get confused by a class called "ObjectA". It's a
> class, not an object.
You guys get worked up over some pretty weird shit.
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-07-20 18:52 +0000 |
| Message-ID | <slrnj2e8uj.gtj.grahn+nntp@frailea.sa.invalid> |
| In reply to | #7976 |
On Tue, 2011-07-19, Noah Roberts wrote:
> On 7/19/2011 6:12 AM, Jorgen Grahn wrote:
>> On Mon, 2011-07-18, Ian Collins wrote:
>>> On 07/19/11 11:44 AM, Noah Roberts wrote:
>>>> On 7/18/2011 4:29 PM, Ian Collins wrote:
>>>>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>>>>> Considder:
>>>>>>
>>>>>> struct ObjectA
>>>>>> {
>> ...
>>
>>>> No, there's a second option: apply the cast operator and call the copy
>>>> operator with the result.
>>>
>>> Your terminology is a little confused. There is a conversion (not cast)
>>> operator and a copy constructor (not operator).
>>
>> And I personally get confused by a class called "ObjectA". It's a
>> class, not an object.
>
> You guys get worked up over some pretty weird shit.
I wasn't worked up. But surely you want your postings to be as clear as
possible?
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | James Kanze <james.kanze@gmail.com> |
|---|---|
| Date | 2011-07-20 12:53 -0700 |
| Message-ID | <69d0a5f3-414d-471c-af2b-367fb2fab235@a31g2000vbt.googlegroups.com> |
| In reply to | #7945 |
On Jul 18, 11:46 pm, Noah Roberts <d...@email.me> wrote:
> Considder:
> struct ObjectA
> {
> ObjectA(ObjectA const&);
> template < typename T >
> ObjectA(T const&);
> void fun() const;
> };
>
> struct ObjectB
> {
> operator ObjectA () const;
> };
> static_cast<ObjectA>(an_object_b).fun();
> Which constructor does the standard specify will be called during the
> static_cast: copy or template?
My first reaction is that it's ambiguous. In both cases
(supposing "an_object_b" has type ObjectB), you need a const
conversion, followed by a user defined conversion. There is
a rule that all other things being equal, a non-template will
have precedence over a template, but I'm not sure it applies
here. And there are all sorts of subtilities involving overload
resolution in the presence of const; I don't have a copy of the
standard here to try to work them out.
> Behavior I am seeing is that the templated version is called and then
> bases are called on the result of the cast operator.
> Is there a way to explicitly call the cast operator or is that what I'm
> supposedly doing?
There's no way of explicitly calling either the conversion
operator or the constructor; the only way either gets called is
because of a conversion (explicit or implicit). My feeling is
that either the conversion is ambiguous, or perhaps the
conversion operator should be called, because of the
non-template over template rule; but without having a copy of
the standard to verify it by, I wouldn't want to swear on it.
--
James Kanze
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web