Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a using statement appear in a constructor initialization list?

How can one incorporate a using statement into a constructor initialization list?

For example, rather than

foo::foo(int a, int b, int c) : a(a), b(b), c(something_long::tada(c)) {}

I would like to have

// Invoking some 'using something_long::tada;' magic
foo::foo(int a, int b, int c) : a(a), b(b), c(tada(c)) {}

Presumably this looks something like the goofy try/catch syntax required in this code region. Functionally, permiting using statements feels important as something_long::tada(c) and using something_long::tada; tada(c) can have different behavior per Koenig lookup.

like image 850
Rhys Ulerich Avatar asked May 08 '13 03:05

Rhys Ulerich


2 Answers

Would a namespace alias help?

using SVLI = something::very::long::indeed;

foo::foo(int a, int b, int c) : a(a), b(b), c(SVLI::tada(c)) {}
like image 139
Paul Mitchell Avatar answered Sep 20 '22 11:09

Paul Mitchell


It depends on how many levels of namespaces you need to type. If you need to type quite a few levels of namespaces, you could bridge it in a static function:

class foo
{
  //...
  static int tada_again(int c)
  {
    return namespaceA::namespaceB::namespaceC::namespaceD::namespaceE::tada(c);
  }
  //...
};

foo::foo(int a, int b, int c) : a(a), b(b), c(tada_again(c))
{
}

If there aren't many levels of namespaces need to be typed, from maintain or code readability point of view, keep clear namespace will be better.

like image 29
billz Avatar answered Sep 18 '22 11:09

billz