Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

non-member functions and const cv

32 views
Skip to first unread message

Popping mad

unread,
Nov 7, 2016, 10:17:58 PM11/7/16
to
I want to make a parameter constant for a function that is not a memeber of a class

template <class outcost>
std::ostream& operator << ( std::ostream &output, state<outcost> const &p ) const
{
output << "nam => " << p.nam() << "\tcost=> " << p.r() << std::endl;
return output;
}


This fails

|| g++ -Wall -ggdb -c nodes.cpp
nodes.h|171 col 86| error: non-member function ‘std::ostream& tree::operator<<(std::ostream&, const tree::state<outcost>&)’ cannot have cv-qualifier
|| friend std::ostream& operator << ( std::ostream &output, state<outcost> const &p ) const ;
|| ^~~~~


If you remove const from the function is also fails..






template <class outcost>
std::ostream& operator << ( std::ostream &output, state<outcost> const &p )
{
output << "nam => " << p.nam() << "\tcost=> " << p.r() << std::endl;
return output;
}

:!make 2>&1| tee /tmp/vjvaPl4/171
g++ -Wall -ggdb -c nodes.cpp
g++ -Wall -ggdb -o fitch.o -c fitch.cpp
In file included from fitch.cpp:3:0:
nodes.h: In instantiation of ‘std::ostream& tree::operator<<(std::ostream&, const tree::state<cost>&) [with outcost = int; std::ostream = std:
:basic_ostream<char>]’:
nodes.h:158:16: required from ‘tree::Pstates<cost>::Pstates(std::vector<tree::state<cost> >) [with cost = int]’
fitch.cpp:15:47: required from here
nodes.h:185:24: error: passing ‘const tree::state<int>’ as ‘this’ argument discards qualifiers [-fpermissive]
output << "nam => " << p.nam() << "\tcost=> " << p.r() << std::endl;

nodes.h:125:15: note: in call to ‘std::__cxx11::string tree::state<cost>::nam() [with cost = int; std::__cxx11::string = std::__cxx11::basic
_string<char>]’
std::string nam(){return _nam;};
^~~
nodes.h:185:51: error: passing ‘const tree::state<int>’ as ‘this’ argument discards qualifiers [-fpermissive]
output << "nam => " << p.nam() << "\tcost=> " << p.r() << std::endl;

nodes.h:126:8: note: in call to ‘cost tree::state<cost>::r() [with cost = int]’


There must be a way to do this.

Alf P. Steinbach

unread,
Nov 7, 2016, 10:24:20 PM11/7/16
to
On 08.11.2016 04:17, Popping mad wrote:
> I want to make a parameter constant for a function that is not a memeber of a class
>
> template <class outcost>
> std::ostream& operator << ( std::ostream &output, state<outcost> const &p ) const
> {
> output << "nam => " << p.nam() << "\tcost=> " << p.r() << std::endl;
> return output;
> }

That's the right approach except for the `const` after the function head.

The `const` on the formal argument is enough.


> This fails
>
> || g++ -Wall -ggdb -c nodes.cpp
> nodes.h|171 col 86| error: non-member function ‘std::ostream& tree::operator<<(std::ostream&, const tree::state<outcost>&)’ cannot have cv-qualifier
> || friend std::ostream& operator << ( std::ostream &output, state<outcost> const &p ) const ;
> || ^~~~~
>
>
> If you remove const from the function is also fails..

That's a different problem.

You need to make the `nam` member function `const`.


Cheers & hth.,

- Alf

ruben safir

unread,
Nov 8, 2016, 3:19:54 AM11/8/16
to
On 11/07/2016 10:21 PM, Alf P. Steinbach wrote:
> On 08.11.2016 04:17, Popping mad wrote:
>> I want to make a parameter constant for a function that is not a
>> memeber of a class
>>
>> template <class outcost>
>> std::ostream& operator << ( std::ostream &output, state<outcost> const
>> &p ) const
>> {
>> output << "nam => " << p.nam() << "\tcost=> " << p.r() <<
>> std::endl;
>> return output;
>> }
>
> That's the right approach except for the `const` after the function head.
>
> The `const` on the formal argument is enough.


that would be an error. You can't use a non-member function on a const
varriable, FWIW. There might not be a means to const this, as long as a
non-member function is working on the variable.

Alf P. Steinbach

unread,
Nov 8, 2016, 9:50:51 AM11/8/16
to
On 08.11.2016 09:19, ruben safir wrote:
>
> that would be an error. You can't use a non-member function on a const
> varriable, FWIW.

I'm sorry, but that doesn't make sense to me.


> There might not be a means to const this, as long as a
> non-member function is working on the variable.

Not sure what this means either, sorry.

ruben safir

unread,
Nov 8, 2016, 11:22:15 AM11/8/16
to
On 11/08/2016 09:48 AM, Alf P. Steinbach wrote:
>>
>> that would be an error. You can't use a non-member function on a const
>> varriable, FWIW.
>
> I'm sorry, but that doesn't make sense to me.
>

A non-member function is one outside of the class.
>
>> There might not be a means to const this, as long as a
>> non-member function is working on the variable.
>
> Not sure what this means either, sorry.
>

Functions from outside of a class can not guarantee through const the
integrity of a function argument.

Scott Lurndal

unread,
Nov 8, 2016, 11:31:33 AM11/8/16
to
ruben safir <ru...@mrbrklyn.com> writes:
>On 11/08/2016 09:48 AM, Alf P. Steinbach wrote:
>>>
>>> that would be an error. You can't use a non-member function on a const
>>> varriable, FWIW.
>>
>> I'm sorry, but that doesn't make sense to me.
>>
>
>A non-member function is one outside of the class.

So what? That doesn't preclude the function from using
const arguments, nor does it preclude the function from
using non-member const variables.

>>
>>> There might not be a means to const this, as long as a
>>> non-member function is working on the variable.
>>
>> Not sure what this means either, sorry.
>>
>
> Functions from outside of a class can not guarantee through const the
>integrity of a function argument.

c'est what?

static void function(const int& a)
{
a = 10; // ERROR
}

/tmp/a.cpp: In function 'void function(const int&)':
/tmp/a.cpp:4: error: assignment of read-only reference 'a'

ruben safir

unread,
Nov 8, 2016, 11:59:31 AM11/8/16
to
On 11/08/2016 11:31 AM, Scott Lurndal wrote:
> So what? That doesn't preclude the function from using
> const arguments,

actually it does, at least as a parameter

Mr Flibble

unread,
Nov 8, 2016, 12:34:12 PM11/8/16
to
What? Either you are not explaining yourself properly or you are totally
wrong.

/Flibble


Popping mad

unread,
Nov 8, 2016, 9:26:13 PM11/8/16
to
On Tue, 08 Nov 2016 16:31:25 +0000, Scott Lurndal wrote:

> c'est what?
>
> static void function(const int& a)
> {
> a = 10; // ERROR
> }

that is not within a class, FWIW.

Popping mad

unread,
Nov 8, 2016, 9:44:14 PM11/8/16
to
On Tue, 08 Nov 2016 17:34:02 +0000, Mr Flibble wrote:

>> actually it does, at least as a parameter
>
> What? Either you are not explaining yourself properly or you are totally
> wrong.

that has happened before and it will again, but that is the documentation
I drummed up on this problem. GCC is saying it is removing the constraints
because it can not guarantee the const within a friend method

In instantiation of ‘std::ostream&
tree::operator<<(std::ostream&, const tree::state<cost>&) [with outcost =
int; std::ostream = std: :basic_ostream<char>]’:
nodes.h:158:16: required from
‘tree::Pstates<cost>::Pstates(std::vector<tree::state<cost> >) [with cost
= int]’
fitch.cpp:15:47: required from here nodes.h:185:24: error: passing
‘const tree::state<int>’ as ‘this’ argument discards qualifiers
[-fpermissive]

http://stackoverflow.com/questions/5973427/error-passing-xxx-as-this-argument-of-xxx-discards-qualifiers

http://www.cplusplus.com/forum/general/35686/
0 new messages