Const Function Weirdness

I got a surprising result with const functions, and I could use some clarification if anyone knows the root of this:

While playing around I made a const member function that passes an output (WordListOut) by parameter:

void UBullCowCartridge::GetValidWordsFromList(const TArray& WordListIn, TArray& WordListOut, int32 MaxWordLength) const

Inside of this function, after pairing down the list of passable words and storing them in a temp TArray called TempWordList, I assign the output parameter WordListOut to the temp array to return it (via reference):

WordListOut = TempWordList;

I also have a member variable:

TArray ValidHiddenWordList = { TEXT(“default”) };

Now, the interesting thing is if I pass this member variable ValidHiddenWordList into the GetValidWordsFromList() function as the output parameter:

GetValidWordsFromList(SomeList, ValidHiddenWordList, SomeInt);

The program will then compile and run without issue, even though I am breaking the const attribute of my member function GetValidWordsFromList() by directly assigning a new value to my member variable ValidHiddenWordList (albeit though an alias).

Is it expected that the compiler doesn’t catch this loophole (I am using VS Community 19)? Is it only smart enough to catch blatant fouls (i.e. ValidHiddenWordList = SomeOtherList) and this is a known limitation?

Thanks in advance for your thoughts.

That would be the issue. Don’t pass it into the function, just use it directly.

Thanks for the reply; I think maybe I didn’t make my finding and/or question clear.

It was an example to describe some unexpected behavior I stumbled upon and found surprising, and which appeared to be a loophole in the concept of a const function. I wrote it expecting the compiler to catch it, and it did not.

The “weirdness” is as follows:

//MyHeader.h

class MyClass
{
public:
int MyInt;
void MyConstFunction(int& OutputParam) const;
void MyNonConstFunction();
}

//MyImplementation.cpp

void MyClass::MyConstFunction((int& OutputParam) const
{
OutputParam = 1;
}

void MyClass::MyNonConstFunction()
{
//This const function is being passed a member variable that it then modifies
MyConstFunction(MyInt);
}

Apparently, the above structure does not return a compiler error, as I found out. My const function is not really const. My intuition was the compiler should have errored out on this (though always possible I am misunderstanding the nuances of a const function).

I was curious if this was a commonly known loophole. Not to determine if it’s something I can abuse, but rather if it’s something I need to watch out for if I say use some random utility library. And if there were other such loopholes that are common knowledge and I need to be aware of.

I got my gears turning on how reliable/enforced is a const declaraction.

Preciate any input.

That is exactly what I understood you were saying.

The compiler would have to perform static analysis to see that you were modifying a member in that code. Both of these functions can be separately compiled.

MyConstFunction does not modify any members, it modifies what is passed in (OutputParam). It has no way of knowing that you actually passed in a member.

MyNonConstFunction Here you are passing a non-const MyInt to a non-const reference. There’s nothing wrong here.


These member functions are just syntactic sugar for the following

void MyConstFunction(const MyClass&, int& OutputParam)
{
    OutputParam = 1;
}

void MyNonConstFunction(MyClass& Thing)
{
    MyConstFunction(Thing, Thing.MyInt);
}

Thing is a non-const reference, you pass it to a function that takes a const MyClass& that doesn’t modify it. Then you pass a non-const MyInt to a int& parameter that does modify, no issues.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms