 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Yakov Zaytev Guest
|
Posted: Fri Oct 14, 2005 5:36 pm Post subject: 'const' with template argument in member function |
|
|
Hi all!
I have a weird problem:
template<class O, class T>
class Foo {
public:
typedef void (O::* set_f) (const T&);
set_f f;
O* o;
// ..
void set (const T& v)
{
// play with v
((*o).*f) (v);
}
// ...
Foo (O* ob, set_f fun )
{
// ..
}
};
[ and when i try to use it like this: ]
class some_class { public: void set (const char* s) { } }
// ..
{
Foo<char*>* f= new Foo<char*> (& some_class_obj, &some_class::set);
}
-------------
I got:
QCC -c -o property.o property.cc
property.cc: In method `void O::set(const char *)':
property.cc:18: assignment to `char *' from `const char *' discards
qualifiers
property.cc: In method `class Property<char *> O::prop_dat()':
property.cc:39: no matching function for call to
`MemFun_PropertyProxy<O,char *>::MemFun_PropertyProxy (O *, char *
(O::*)() const, void (O::*)(const char *))'
.../Property.h:35: candidates are: MemFun_PropertyProxy<O,char
*>::MemFun_PropertyProxy(O *, char * (O::*)() const, void (O::*)(char
*const &))
.../Property.h:60: MemFun_PropertyProxy<O,char
*>::MemFun_PropertyProxy(const MemFun_PropertyProxy<O,char *> &)
cc: /usr/qnx630/host/qnx6/x86/usr/lib/gcc-lib/ntox86/2.95.3/cc1plus error 33
make: *** [property.o] Error 1
WHY const T& [with T=char*] evaluates to char* const& [!!] ??
Could Anybody explain me what is happen here ??
thanks
/wbr
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Oct 15, 2005 12:56 pm Post subject: Re: 'const' with template argument in member function |
|
|
Yakov Zaytev <iakov (AT) inbox (DOT) ru> wrote:
<snip>
| Quote: | WHY const T& [with T=char*] evaluates to char* const& [!!] ??
snip |
Templates do not provide textual substitution, like macros do. The
const qualification applies to T as a whole, i.e. to the pointer type
char *, and not to the type it is based on, char.
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sat Oct 15, 2005 1:04 pm Post subject: Re: 'const' with template argument in member function |
|
|
Yakov Zaytev wrote:
| Quote: | Hi all!
I have a weird problem:
template<class O, class T
class Foo {
public:
typedef void (O::* set_f) (const T&);
set_f f;
O* o;
// ..
void set (const T& v)
{
// play with v
((*o).*f) (v);
}
// ...
Foo (O* ob, set_f fun )
{
// ..
}
};
[ and when i try to use it like this: ]
class some_class { public: void set (const char* s) { } }
// ..
{
Foo
}
-------------
I got:
QCC -c -o property.o property.cc
property.cc: In method `void O::set(const char *)':
property.cc:18: assignment to `char *' from `const char *' discards
qualifiers
property.cc: In method `class Property<char *> O::prop_dat()':
property.cc:39: no matching function for call to
`MemFun_PropertyProxy<O,char *>::MemFun_PropertyProxy (O *, char *
(O::*)() const, void (O::*)(const char *))'
../Property.h:35: candidates are: MemFun_PropertyProxy<O,char
*>::MemFun_PropertyProxy(O *, char * (O::*)() const, void (O::*)(char
*const &))
../Property.h:60: MemFun_PropertyProxy<O,char
*>::MemFun_PropertyProxy(const MemFun_PropertyProxy<O,char *> &)
cc: /usr/qnx630/host/qnx6/x86/usr/lib/gcc-lib/ntox86/2.95.3/cc1plus error 33
make: *** [property.o] Error 1
WHY const T& [with T=char*] evaluates to char* const& [!!] ??
Could Anybody explain me what is happen here ??
thanks
/wbr
|
The sample code posted is clearly not the code causing the error. Nor
does the error posted have any bearing on the question asked. On the
basis of the error message alone, I would conclude that the program
fails to compile becuase it is attempting to pass a pointer to a const
member function where the only potentially matching declaration
specifies a pointer to a non const member function.
Specifically:
void (O::*)( const char *)
and
void (O::*)(const char *) const
are two distinct member function pointer types. Neither one implicitly
converts to the other. Therefore a function that wishes to accept
either type of pointer should probably be overloaded for each of the
two types.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Oct 15, 2005 1:05 pm Post subject: Re: 'const' with template argument in member function |
|
|
Yakov Zaytev <iakov (AT) inbox (DOT) ru> writes:
| Quote: | WHY const T& [with T=char*] evaluates to char* const& [!!] ??
|
'const T &' is equivalent to the more intelligible 'T const &'. Now
substitute 'char *' for T.
What would you expect?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Sun Oct 16, 2005 9:43 am Post subject: Re: 'const' with template argument in member function |
|
|
"Yakov Zaytev" <iakov (AT) inbox (DOT) ru> wrote
[...]
| Quote: | template<class O, class T
class Foo {
public:
typedef void (O::* set_f) (const T&);
[...]
Foo (O* ob, set_f fun )
[...]
class some_class { public: void set (const char* s) { } }
[...]
Foo
|
When you ask a question like this you need to type a piece of code that people
can compile. The above has obvious typos.
[...]
| Quote: | property.cc: In method `void O::set(const char *)':
property.cc:18: assignment to `char *' from `const char *' discards
qualifiers
|
This basically tells what the problem is. There is no match for the supplied
arguments to the template.
[...]
| Quote: | WHY const T& [with T=char*] evaluates to char* const& [!!] ??
|
Because after expansion of the template arguments "const" refers to pointer
(char * const), not to char as in the signature of the function (char const *)
-- Gene
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Sun Oct 16, 2005 9:45 am Post subject: Re: 'const' with template argument in member function |
|
|
Hi
Yakov Zaytev wrote:
| Quote: | Hi all!
I have a weird problem:
template<class O, class T
class Foo {
[...]
class some_class { public: void set (const char* s) { } }
// ..
{
Foo
|
That shouldn't compile. As far as I can see, Foo has two template parameters
(Maybe the second one has a default value?). Anyway:
| Quote: | WHY const T& [with T=char*] evaluates to char* const& [!!] ??
Could Anybody explain me what is happen here ??
|
Because. If you say you want a const reference to T (with T=char*) you will
get a const reference to T, i.e. char * const &.
Why don't you use strings, btw?
Markus
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|