Topic: casting away const


Author: Jim Cobban <thesnaguy@hotmail.com>
Date: 2000/01/22
Raw View
This is a multi-part message in MIME format.
--------------0317AF0DC4B0ED5B3FCBB3DA
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

sujh wrote:

> The following example has taken from Josuttis's. When I compiled the code,
> I
> got errors. I think I have to cast away const. However, out of three or
> four different casting style provided in c++, I'm not exactly sure how that
> can be done or which one has to be used. Could anyone make changes to the
> code to
> show how this problem can be worked out?

....

> void foo (const std::vector<Person>& coll)
> {
>     using std::for_each;
>     using std::bind2nd;
>     using std::mem_fun_ref;
>
>     for_each (coll.begin(), coll.end(),
>               mem_fun_ref(&Person::print));
>
>     for_each (coll.begin(), coll.end(),
>               bind2nd(mem_fun_ref(&Person::printWithPrefix),
>                       "person: "));
> }

The problem in both places would appear to be that mem_fun_ref in your compiler
does not appear to have a version for a const member function which is required
by section 20.3.8 of the standard.  Check your copy of the <functional> header
to see what definitions are present.  You could use a const_cast to remove the
const, but it would be really ugly (of course it is supposed to be), and I
would only do it if you can't get a response from your compiler supplier.

--
Jim Cobban   jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438


--------------0317AF0DC4B0ED5B3FCBB3DA
Content-Type: text/x-vcard; charset=us-ascii;
 name="thesnaguy.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Jim Cobban
Content-Disposition: attachment;
 filename="thesnaguy.vcf"

begin:vcard
n:Cobban;James
tel;fax:+1-613-592-9438
tel;home:+1-613-592-9438
x-mozilla-html:FALSE
url:http://www.magma.ca/~jcobban
version:2.1
email;internet:thesnaguy@hotmail.com
title:Consultant
adr;quoted-printable:;;34 Palomino Dr.=0D=0A;Kanata;ON;K2M 1M1;Canada
fn:Jim Cobban
end:vcard

--------------0317AF0DC4B0ED5B3FCBB3DA--

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "sujh" <suj_h@yahoo.com>
Date: 2000/01/17
Raw View
The following example has taken from Josuttis's. When I compiled the code,
I
got errors. I think I have to cast away const. However, out of three or
four different casting style provided in c++, I'm not exactly sure how that
can be done or which one has to be used. Could anyone make changes to the
code to
show how this problem can be worked out?
Thanks.

Compiler pukes:
test.cpp(30) : error C2664: 'mem_fun_ref' : cannot convert parameter 1 from
'voi
d (__thiscall Person::*)(void) const' to 'void (__thiscall
Person::*)(void)'
        Types pointed to are unrelated; conversion requires
reinterpret_cast, C-
style cast or function-style cast
test.cpp(35) : error C2784: 'class std::mem_fun_ref_t<_R,_Ty> __cdecl
std::mem_f
un_ref(_R (__thiscall _Ty::*)(void))' : could not deduce template argument
for '
<Unknown>' from 'void (__thiscall Person::*)(class
std::basic_string<char,struct
 std::char_traits<char>,class std::allocator<char> >) const'


// code starts here
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>


class Person {
  private:
    std::string name;
  public:
    //...
    void print () const {
        std::cout << name << std::endl;
    }
    void printWithPrefix (std::string prefix) const {
        std::cout << prefix << name << std::endl;
    }
};

void foo (const std::vector<Person>& coll)
{
    using std::for_each;
    using std::bind2nd;
    using std::mem_fun_ref;

    // call member function print() for each element
    for_each (coll.begin(), coll.end(),
              mem_fun_ref(&Person::print));

    // call member function printWithPrefix() for each element
    // - "person: " is passed as an argument to the member function
    for_each (coll.begin(), coll.end(),
              bind2nd(mem_fun_ref(&Person::printWithPrefix),
                      "person: "));
}


int main()
{
    std::vector<Person> coll(5);
    foo(coll);
 return 0;
}


--+su+--
http://www.geocities.com/cpu8031/

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]