Const XX discards qualifiers [- fpermissive]

In the code snippet below, mKnownSRList is defined as follows:

std::vector<EndPointAddr*> mKnownSRList; 

I get the compilation error shown in code snippet 2. Can you tell me what is wrong with this code, please? The contents of the getTipcAddress () and compareTo functions are shown in code snippets 3 and 4. below.

CODE SNIPPET 1 (compilation error noted)

 void ServiceRegistrarAPI::removeKnownSR(EndPointAddr & srEndPointAddr) { auto last = std::remove_if(mKnownSRList.begin(), mKnownSRList.end(), [srEndPointAddr]( EndPointAddr* o ) { //LINE 355 is the following EndPointTipcAddr myTipcAddress = srEndPointAddr.getTipcAddress(); EndPointTipcAddr otherTipcAddress = o->getTipcAddress(); return (myTipcAddress.compareTo(otherTipcAddress)); }); if(*last != nullptr) { delete *last; } mKnownSRList.erase(last, mKnownSRList.end()); } 

SNIPPET 2 (compilation error)

  ServiceRegistrarAPI.cpp:355:72: error: passing 'const EndPointAddr' as 'this' argument of 'EndPointTipcAddr& EndPointAddr::getTipcAddress()' discards qualifiers [- fpermissive] 

CODE SNIPPET 3 (getTipcAddress function)

 EndPointTipcAddr & getTipcAddress() { return mTipcAddress; } 

NIPPET 4 CODE (compareTo function)

  bool EndPointTipcAddr::compareTo(EndPointTipcAddr &rhs) { if( (mType == rhs.getType()) && (mInstanceNo == rhs.getInstanceNo()) ) { return true; } return false; } 
+4
source share
4 answers

The predicate function (third argument to std::remove_if ) is not allowed to modify the object. All methods called an iterator must be const . See this documentation :

A function must not change its argument.

You can set getTipcAddress as const if you return a copy of the value or a const pointer.

+3
source

See S5.1.2.5:

The closure type for the lambda expression has a public built-in function (13.5.4), the parameters of which and the return type are described by suggesting the parameter-declaration-lambda expression and trailingreturn-type, respectively. This function call statement declared const (9.3.1) if and only if lambda expressions parameter-declaration-sentence should not be mutable. It is not virtual or declared mutable. The default arguments (8.3.6) must not be specified in the declaration of the lambdadeclarator declaration parameter. Any exception specification specified in a lambda expression applies to the corresponding function call statement. attribute-specifier-seq in lambda-declarator refers to the type of the corresponding function call statement. [Note: Names referenced in the lambda declarator are viewed in the context in which the lambda expression appears. -end note]

Basically, the implication is the functor operator (), which is generated by default, const , and you captured by value, and this captured variable is a member of the generated functor.

So, you have two options:

  • Capture by reference, not by value.
  • Change your lambda to the following (note mutable after the parameter declaration is suggested):

    [srEndPointAddr](EndPointAddr* o) mutable { ... }

+5
source

In short, you will get this error, because in this case you are calling the non-const method for the const instance: srEndPointAddr is const, but you are calling the non-const getTipcAddress method on it. The solution for you would be to declare this method const, as it seems to be a simple getter and probably will not modify the object.

0
source

This is the answer to how to solve the problem and not why it makes a mistake (thanks juanchopanza)

I assume you have a problem with constant constructors.

Add const -qualified overload for getTipcAddress :

 EndPointTipcAddr & getTipcAddress() const { return mTipcAddress; } ~~~~~ 

and add const at the end of compareTo :

 bool EndPointTipcAddr::compareTo(EndPointTipcAddr &rhs) const ~~~~~ 
0
source

All Articles