C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const T und T bei Template Parameter Auflösung

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Maximilian Hrabowski
Guest





PostPosted: Mon Oct 04, 2004 4:12 pm    Post subject: const T und T bei Template Parameter Auflösung Reply with quote



Hallo allerseits,

bitte werft einen Blick auf folgendes Programm-Fragment.

Das ganze demonstriert die Nutzung eines Funktionsobjektes
"mem_fun_ref_wrapper_t" über die template funtion mem_fun_ref_wrap.

mem_fun_ref_wrapper_t ist ein Functionsobjekt das es ermöglicht
Rückgabewerte von Methoden eines Objektes als Parameter an ein anderes
Funktionsobject zu übergeben, das normalerweise den Objekttyp als
Parameter erhalten würde, z.B:

class Foo
{
Foo();
public:
int number() const;
};

std::vector<Foo> myFoos;

std::find_if( myFoos.begin(), myFoos.end(), std::mem_fun_ref_wrap(
bind1st( std::equal_to<int>(), 2 ), &Foo::number ) )

liefert iterator auf erstes Foo das für number() 2 zurückgibt.

Das Programm compiliert fehlerfrei mit gcc 3.3.3 und tut das Erwartete.

Mit MSVC++.NET hingegen gibts leider folgende Fehler:

<ANFANG>
d:cvsrootteststestProject.cpp(108) : error C2782:
'std::mem_fun_ref_wrapper_t<Operation,ObjectType>
std::mem_fun_ref_wrap(const Operation &,Operation::argument_type
(__thiscall ObjectType::* )(void))' : template parameter 'ObjectType' is
ambiguous
d:cvsrootteststestProject.cpp(86) : see declaration of
'std::mem_fun_ref_wrap'
could be 'const MyStruct'
or 'MyStruct'
d:cvsrootteststestProject.cpp(108) : error C2785:
'std::const_mem_fun_ref_wrapper_t<Operation,ObjectType>
std::mem_fun_ref_wrap(const Operation &,Operation::argument_type
(__thiscall ObjectType::* )(void) const)' and '<Unknown>' have different
return types
d:cvsrootteststestProject.cpp(79) : see declaration of
'std::mem_fun_ref_wrap'
d:cvsrootteststestProject.cpp(116) : error C2782:
'std::mem_fun_ref_wrapper_t<Operation,ObjectType>
std::mem_fun_ref_wrap(const Operation &,Operation::argument_type
(__thiscall ObjectType::* )(void))' : template parameter 'ObjectType' is
ambiguous
d:cvsrootteststestProject.cpp(86) : see declaration of
'std::mem_fun_ref_wrap'
could be 'const MyStruct'
or 'MyStruct'
d:cvsrootteststestProject.cpp(116) : error C2785:
'std::const_mem_fun_ref_wrapper_t<Operation,ObjectType>
std::mem_fun_ref_wrap(const Operation &,Operation::argument_type
(__thiscall ObjectType::* )(void) const)' and '<Unknown>' have different
return types
d:cvsrootteststestProject.cpp(79) : see declaration of
'std::mem_fun_ref_wrap'
d:cvsrootteststestProject.cpp(124) : error C2782:
'std::mem_fun_ref_wrapper_t<Operation,ObjectType>
std::mem_fun_ref_wrap(const Operation &,Operation::argument_type
(__thiscall ObjectType::* )(void))' : template parameter 'ObjectType' is
ambiguous
d:cvsrootteststestProject.cpp(86) : see declaration of
'std::mem_fun_ref_wrap'
could be 'const MyStruct'
or 'MyStruct'
d:cvsrootteststestProject.cpp(124) : error C2785:
'std::const_mem_fun_ref_wrapper_t<Operation,ObjectType>
std::mem_fun_ref_wrap(const Operation &,Operation::argument_type
(__thiscall ObjectType::* )(void) const)' and '<Unknown>' have different
return types
d:cvsrootteststestProject.cpp(79) : see declaration of
'std::mem_fun_ref_wrap'
<ENDE>

Meine Fragen nach Priorität:

1. Was muss ich anders machen, damit der MSVC das ganze kapiert.
2. Ist der Code so korrekt und MSVC zu pingelig oder ist der gcc zu
tolerant (und rät gut)
3. Lässt sich mem_fun_ref_wrapper_t durch standard functionsobjekte
(eher aus einer Kombination derselben) aus der STL ersetzen?

Hier kommt das Programm:

<ANFANG>
#include <iostream>
#include <vector>
#include <cmath>
#include <sstream>
#include <cassert>

#include <algorithm>
#include <iterator>
#include <functional>

class MyStruct
{
public:
MyStruct( int i=100 ) : m_i(i) {}
int number() const { return m_i; }
private:
int m_i;
};

std::ostream& operator<<( std::ostream& out, const MyStruct& ms )
{
out << ms.number();
return out;
}

namespace std
{
template OutputIter copy_if( InputIter b, InputIter e, OutputIter dest,
Predicate Pred )
{
while( b != e )
{
if( Pred(*b) )
{
*dest = *b;
++dest;
}
++b;
}
return dest;
}

template<class Operation, class ObjectType>
struct mem_fun_ref_wrapper_t : public
std::unary_function<ObjectType,typename Operation::result_type>
{
mem_fun_ref_wrapper_t( const Operation& op, typename
Operation::argument_type (ObjectType::*funcPtr)() ) :
m_op(op),
m_funcPtr(funcPtr)
{}

typename Operation::result_type operator()( ObjectType& ref )
{
return m_op( (ref.*m_funcPtr)() );
}

Operation m_op;
typename Operation::argument_type (ObjectType::*m_funcPtr)();
};

template<class Operation, class ObjectType>
struct const_mem_fun_ref_wrapper_t : public
std::unary_function<ObjectType,typename Operation::result_type>
{
const_mem_fun_ref_wrapper_t( const Operation& op, typename
Operation::argument_type (ObjectType::*funcPtr)() const ) :
m_op(op),
m_funcPtr(funcPtr)
{}

typename Operation::result_type operator()( const ObjectType& ref )
{
return m_op( (ref.*m_funcPtr)() );
}

Operation m_op;
typename Operation::argument_type (ObjectType::*m_funcPtr)() const;
};

template<class Operation, class ObjectType> inline
const_mem_fun_ref_wrapper_t<Operation,ObjectType>
mem_fun_ref_wrap( const Operation& op, typename
Operation::argument_type (ObjectType::*mf)() const )
{
return const_mem_fun_ref_wrapper_t<Operation,ObjectType>(op, mf );
}

template<class Operation, class ObjectType> inline
mem_fun_ref_wrapper_t<Operation,ObjectType>
mem_fun_ref_wrap( const Operation& op, typename
Operation::argument_type (ObjectType::*mf)() )
{
return mem_fun_ref_wrapper_t<Operation,ObjectType>(op, mf );
}

}

int main(int argc, char* argv[])
{
int array[] = {0,1,2,3,4,5,6,7,8,9,10};

std::vector<MyStruct> myNumbers;
std::copy( array, array+10, std::back_inserter(myNumbers) );

std::cout << "Array: ";
std::transform( myNumbers.begin(), myNumbers.end(),
std::ostream_iterator &MyStruct::number ) );
std::cout << std::endl;

std::cout << "Greater 2: ";
std::copy_if( myNumbers.begin(),
myNumbers.end(),
std::ostream_iterator std::mem_fun_ref_wrap( std::bind2nd( std::greater<int>(), 2),
&MyStruct::number )
);
std::cout << std::endl;

std::cout << "Less 2: ";
std::copy_if( myNumbers.begin(),
myNumbers.end(),
std::ostream_iterator std::mem_fun_ref_wrap( std::bind2nd( std::less<int>(), 2),
&MyStruct::number )
);
std::cout << std::endl;

std::cout << "Equal 2: ";
std::copy_if( myNumbers.begin(),
myNumbers.end(),
std::ostream_iterator std::mem_fun_ref_wrap( std::bind1st( std::equal_to<int>(), 2),
&MyStruct::number )
);

std::cout << std::endl;
}

Besten Dank,
Maxim

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Markus Schaaf
Guest





PostPosted: Mon Oct 04, 2004 7:55 pm    Post subject: Re: const T und T bei Template Parameter Auflösung Reply with quote



"Maximilian Hrabowski" <spam (AT) clausmark (DOT) com> schrieb:

Quote:
std::find_if( myFoos.begin(), myFoos.end(), std::mem_fun_ref_wrap(
bind1st( std::equal_to<int>(), 2 ), &Foo::number ) )

Eine Volltextsuche in meiner Version von ISO-14882 findet keine
Stelle, die den Bezeichner »mem_fun_ref_wrap« enthält.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
M. Hrabowski
Guest





PostPosted: Mon Oct 04, 2004 11:37 pm    Post subject: Re: const T und T bei Template Parameter Auflösung Reply with quote



Markus Schaaf wrote:

Quote:
"Maximilian Hrabowski" <spam (AT) clausmark (DOT) com> schrieb:

std::find_if( myFoos.begin(), myFoos.end(), std::mem_fun_ref_wrap(
bind1st( std::equal_to<int>(), 2 ), &Foo::number ) )

Eine Volltextsuche in meiner Version von ISO-14882 findet keine
Stelle, die den Bezeichner »mem_fun_ref_wrap« enthält.


Wundert mich nicht, ist ja auch nicht Bestandteil der STL, sondern selbst
gechrieben (Deshalb source weiter unten im posting) ;-)

Das kurze Beispiel war nur zur Erläuterung was da weiter unten noch kommt
(nach den Fehlermeldungen). "Das folgende" war wohl im Anfangstext etwas
missverständlich ausgesdrückt, also bitte ganz nach unten scrollen.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Maximilian Hrabowski
Guest





PostPosted: Tue Oct 05, 2004 8:15 am    Post subject: Re: Namensraum ::std Reply with quote

ok, dass man den namespace std nicht erweitern darf war mir nicht bekannt.
Ich habe den namespace, in dem die betreffenden Teile stehen mal in
"std2" geändert.

Das Hauptproblem, nämlich das ich besagten Code am Ende meines ersten
Postings mit MSVC nicht compiliert bekomme, ist leider dasselbe
geblieben und blieb dadurch unbeeinflusst.



Quote:
"M. Hrabowski" <mhrab (AT) ira (DOT) uka.de> writes:

std::mem_fun_ref_wrap

ist ja auch nicht Bestandteil der STL, sondern selbst
gechrieben


"It is undefined for a C++ program to add declarations or
definitions to namespace std or namespaces within
namespace std unless otherwise specified."

ISO/IEC 14882:1998(E), 17.4.3.1 Reserved names

(Und eine "STL" ist auch nicht Teil der von der genannten Norm
beschriebenen Sprache. Du meinst vielleicht die
Standardbibliothek.)


--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Markus Schaaf
Guest





PostPosted: Tue Oct 05, 2004 3:11 pm    Post subject: Re: Namensraum ::std Reply with quote

"Maximilian Hrabowski" <spam (AT) clausmark (DOT) com> schrieb:

Quote:
Das Hauptproblem, nämlich das ich besagten Code am Ende meines ersten
Postings mit MSVC nicht compiliert bekomme, ist leider dasselbe
geblieben und blieb dadurch unbeeinflusst.

Zeigst Du bitte trotzdem den minimalen, vollständigen, neuen Code?
Vorzugsweise gleich mit brauchbarer Formatierung. (Ich diskutiere
nicht gern über "undefined behaviour", und ich korrigiere nicht
gern fremden Code.)

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Maximilian Hrabowski
Guest





PostPosted: Tue Oct 05, 2004 4:03 pm    Post subject: Re: Namensraum ::std Reply with quote

Markus Schaaf wrote:

Quote:
Zeigst Du bitte trotzdem den minimalen, vollständigen, neuen Code?
Vorzugsweise gleich mit brauchbarer Formatierung. (Ich diskutiere
nicht gern über "undefined behaviour", und ich korrigiere nicht
gern fremden Code.)


Gerne, habe ihn nochmal soweit wie möglich gekürzt:

Hier erst noch mal die zugehörigen Fehlermeldungen:

d:cvsrootteststestProject.cpp(82) : error C2782: 'std2::mem_fun_ref_wrapper_t<Operation,ObjectType> std2::mem_fun_ref_wrap(const
Operation &,Operation::argument_type (__thiscall ObjectType::* )(void))' : template parameter 'ObjectType' is ambiguous
d:cvsrootteststestProject.cpp(64) : see declaration of 'std2::mem_fun_ref_wrap'
could be 'const MyStruct'
or 'MyStruct'
d:cvsrootteststestProject.cpp(82) : error C2785: 'std2::const_mem_fun_ref_wrapper_t<Operation,ObjectType>
std2::mem_fun_ref_wrap(const Operation &,Operation::argument_type (__thiscall ObjectType::* )(void) const)' and '<Unknown>' have
different return types
d:cvsrootteststestProject.cpp(57) : see declaration of 'std2::mem_fun_ref_wrap'

Und hier noch mal der Code:

<ANFANG>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

class MyStruct
{
public:
MyStruct( int i=100 ) : m_i(i) {}
int number() const { return m_i; }
private:
int m_i;
};

namespace std2
{
// einmal for non-const methoden
template<class Operation, class ObjectType>
struct mem_fun_ref_wrapper_t : public std::unary_function<ObjectType,typename Operation::result_type>
{
mem_fun_ref_wrapper_t( const Operation& op, typename Operation::argument_type (ObjectType::*funcPtr)() ) :
m_op(op),
m_funcPtr(funcPtr)
{}

typename Operation::result_type operator()( ObjectType& ref )
{
return m_op( (ref.*m_funcPtr)() );
}

Operation m_op;
typename Operation::argument_type (ObjectType::*m_funcPtr)();
};

// einmal für const-methoden
template<class Operation, class ObjectType>
struct const_mem_fun_ref_wrapper_t : public std::unary_function<ObjectType,typename Operation::result_type>
{
const_mem_fun_ref_wrapper_t(const Operation& op, typename Operation::argument_type (ObjectType::*funcPtr)() const):
m_op(op),
m_funcPtr(funcPtr)
{}

typename Operation::result_type operator()( const ObjectType& ref )
{
return m_op( (ref.*m_funcPtr)() );
}

Operation m_op;
typename Operation::argument_type (ObjectType::*m_funcPtr)() const;
};

// methoden zur einfachen Anwendung ala "bind1st" fuer "binder1st"

template<class Operation, class ObjectType> inline
const_mem_fun_ref_wrapper_t<Operation,ObjectType>
mem_fun_ref_wrap( const Operation& op, typename Operation::argument_type (ObjectType::*mf)() const )
{
return const_mem_fun_ref_wrapper_t<Operation,ObjectType>(op, mf );
}

template<class Operation, class ObjectType> inline
mem_fun_ref_wrapper_t<Operation,ObjectType>
mem_fun_ref_wrap( const Operation& op, typename Operation::argument_type (ObjectType::*mf)() )
{
return mem_fun_ref_wrapper_t<Operation,ObjectType>(op, mf );
}

}

int main(int , char**)
{
int array[] = {0,1,2,3,4,5,6,7,8,9,10};

std::vector<MyStruct> myNumbers;
std::copy( array, array+10, std::back_inserter(myNumbers) );

std::cout << "Erster MyStruct.number() groesser 2: ";

std::vector std::find_if( myNumbers.begin(), myNumbers.end(),
std2::mem_fun_ref_wrap( std::bind2nd( std::greater<int>(), 2), &MyStruct::number )
);

if( it != myNumbers.end() )
std::cout << it->number();

std::cout << std::endl;
return 0;
}

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.