Search code examples
c++c++11friend-functionuser-defined-literals

C++0x, user-defined literals with friend operator ""()


Will it be possible and/or useful to define an operator "" (...) as a friend function?

class Puzzle {
  friend Puzzle operator "" _puzzle(const char*, size_t);
  ...
};
void solve(Puzzle);
int main() {
  solve("oxo,xox"_puzzle);
};

I am thinking about "useful" especially, because of the rule that operator "" shall be defined in a namespace only -- not the least because identifiers beginning with _ are reserved in global namespace. Is this friend breaking this rule here? So, there would be no benefit with this not-quite encapsulation, right?


Solution

  • The Standard addresses this directly in the only place it mentions any restrictions on the declarations of user-defined literals, §13.5.8/2:

    A declaration whose declarator-id is a literal-operator-id shall be a declaration of a namespace-scope function or function template (it could be a friend function (11.3)), an explicit instantiation or specialization of a function template, or a using-declaration (7.3.3).

    If the friend is also declared at namespace scope, then there is no distinction between definition in class or namespace scope. Note that there is no requirement for definition at namespace scope, which your question as currently worded asserts.

    If not declared at namespace scope, since it cannot be found by ADL, the friend could be used privately inside the class where it is scope by regular unqualified name lookup. This is the only way of declaring a literal operator which is not an external interface.

    If the friend is defined inside a class template, then two instantiations of the template will generate two identically-named functions at namespace scope, which collide even though they are both invisible outside the class scope.