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

Namespace scopes and function implementation confusion

26 views
Skip to first unread message

Juha Nieminen

unread,
Jun 19, 2019, 6:40:12 AM6/19/19
to
If I had been asked if this compiles, I would have answered "no".

At least clang disagrees with me. I assume it's right. And I'm both
wrong and very confused.

//---------------------------------------------------------
#include <iostream>

namespace ANamespace
{
struct Data
{
int mValue;
bool operator<(const Data&) const;
};
}

class AClass
{
public:
using Inner = ANamespace::Data;
};

// This compiles???
bool AClass::Inner::operator<(const Data& rhs) const
{
return mValue < rhs.mValue;
}

int main()
{
AClass::Inner d1 { 5 }, d2 { 10 };
std::cout << (d1 < d2) << "\n";

ANamespace::Data d3 { 20 }, d4 { 15 };
std::cout << (d3 < d4) << "\n";
}
//---------------------------------------------------------

Marcel Mueller

unread,
Jun 19, 2019, 12:53:30 PM6/19/19
to
Am 19.06.19 um 12:40 schrieb Juha Nieminen:
> class AClass
> {
> public:
> using Inner = ANamespace::Data;
> };
>
> // This compiles???
> bool AClass::Inner::operator<(const Data& rhs) const
> {
> return mValue < rhs.mValue;
> }

Why should it fail?
AClass::Inner and ANamespace::Data are the same type.

Juha Nieminen

unread,
Jun 20, 2019, 5:35:50 AM6/20/19
to
Marcel Mueller <news.5...@spamgourmet.org> wrote:
> Am 19.06.19 um 12:40 schrieb Juha Nieminen:
>> class AClass
>> {
>> public:
>> using Inner = ANamespace::Data;
>> };
>>
>> // This compiles???
>> bool AClass::Inner::operator<(const Data& rhs) const
>> {
>> return mValue < rhs.mValue;
>> }
>
> Why should it fail?

Because Data is not an inner class of AClass?

Marcel Mueller

unread,
Jun 20, 2019, 11:27:47 AM6/20/19
to
Am 20.06.19 um 11:35 schrieb Juha Nieminen:
Ah, you refer to the 'const Data&'. It is an inner class of
AClass::Inner. And this is the scope of the function.


Marcel

Juha Nieminen

unread,
Jun 24, 2019, 7:07:29 AM6/24/19
to
Marcel Mueller <news.5...@spamgourmet.org> wrote:
> Am 20.06.19 um 11:35 schrieb Juha Nieminen:
>> Marcel Mueller <news.5...@spamgourmet.org> wrote:
>>> Am 19.06.19 um 12:40 schrieb Juha Nieminen:
>>>> class AClass
>>>> {
>>>> public:
>>>> using Inner = ANamespace::Data;
>>>> };
>>>>
>>>> // This compiles???
>>>> bool AClass::Inner::operator<(const Data& rhs) const
>>>> {
>>>> return mValue < rhs.mValue;
>>>> }
>>>
>>> Why should it fail?
>>
>> Because Data is not an inner class of AClass?
>
> Ah, you refer to the 'const Data&'.

No. operator<() is not a member function of AClass::Inner, because
AClass::Inner isn't a class at all. operator<() a member function of
ANamespace::Data. It thus feels extremely counter-intuitive to
define it as a member function of AClass::Inner, which is not a
class of any kind.

Incidentally, this won't compile:

bool AClass::Inner::operator<(const Inner& rhs) const

but this does:

bool AClass::Inner::operator<(const AClass::Inner& rhs) const
0 new messages