Home > Mobile >  What are the use-cases of "complete-class context"? What's the benefit that a "c
What are the use-cases of "complete-class context"? What's the benefit that a "c

Time:10-07

In current working draft, the definition of complete-class context is: (§ 11.4.1 [class.mem.general]/7):

A complete-class context of a class (template) is a

  • (7.1) function body ([dcl.fct.def.general]),
  • (7.2) default argument ([dcl.fct.default]),
  • (7.3) default template argument ([temp.param]),
  • (7.4) noexcept-specifier ([except.spec]), or
  • (7.5) default member initializer

within the member-specification of the class or class template.

In fact, I don't understand the intention of this wording. What are the use-cases of "complete-class context"? What's the benefit that a "complete-class context" provides? So any further explanation will be appreciated.

For instance, what does mean that a function body is a complete-class context? Is this mean, for example, ..

struct X {
  void f(){
   X x{};
  }
};

.. that I can create an X object within the function body of X::f? if yes, does it mean anything else?

CodePudding user response:

If we did not have a complete-class context then we could not write a class like

struct foo
{
    void bar() { std::cout << kitty; }
    std::string kitty = "meow";
};

because kitty isn't known about until after bar is defined. You would have to write the class like

struct foo
{
    void bar();
    std::string kitty = "meow";
};

void foo::bar() { std::cout << kitty; }
  • Related