Scope resolution operator being used twice
Asked Answered
U

1

14
namespace libzerocoin {

//Commitment class
Commitment::Commitment::Commitment(const IntegerGroupParams* p,
                               const Bignum& value): params(p), contents(value) {
this->randomness = Bignum::randBignum(params->groupOrder);
this->commitmentValue = (params->g.pow_mod(this->contents, params->modulus).mul_mod(
                         params->h.pow_mod(this->randomness, params->modulus), params->modulus));
}

I just encountered this function definition on GitHub.

I assume that the second and the third "Commitment" refer to the class name and constructor, but I can't figure out the meaning of the first. I am sure that it does not refer to the namespace because that name is different. I have seen the scope resolution operator being used twice in examples, but those refer to nested namespaces.

Underarm answered 30/11, 2017 at 13:50 Comment(1)
Even class B { B(); }; B::B::B::B() {} compiles with gcc and clang but MSVC has issues. Interesting. Maybe injected type.Constructive
E
17

In C++ classes have the feature of having their name injected into their scope ([class]/2):

The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

And the code snippet you showed makes use of it. In certain contexts Commitment::Commitment names the class itself, and in others names the c'tor. Only the last Commitment(, where you open the parentheses, begins the c'tor definition.

And it can look much much worse:

struct foo {
    foo();
};

foo::foo::foo::foo() = default;

Which you can see is valid C++ Live.

Exposed answered 30/11, 2017 at 14:0 Comment(2)
what's the purpose of this strange feature?Enyo
@DevNull - I'd wager it's to smooth certain sharp edges of generic code. But honestly, I never stumbled upon it.Exposed

© 2022 - 2024 — McMap. All rights reserved.