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


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

Re: static_cast, cast operator, and constructors.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: static_cast, cast operator, and constructors.
Date Tue, 19 Jul 2011 11:29:11 +1200
Lines 41
Message-ID <98ju0cFl78U3@mid.individual.net> (permalink)
References <4e24b4fd$0$11125$cc2e38e6@news.uslec.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net lBd6jcE26n3eKjjGDMwCwQNVWPW+u8m9ybc4F0xuQaZ6sfC9Um
Cancel-Lock sha1:0dvcTzWBg6g4MkGsIVHb3iBttfI=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <4e24b4fd$0$11125$cc2e38e6@news.uslec.net>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:7946

Show key headers only | View raw


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

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


Thread

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

csiph-web