6

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.

Rhys Ulerich
  • 1,242
  • 1
  • 12
  • 28

3 Answers3

3

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)) {}
Paul Mitchell
  • 3,241
  • 1
  • 19
  • 22
  • That surmounts my length complaint, say when I use `SVLI` repeatedly within a hypothetical `c1`, `c2`, `c3` all like `c` above, but it doesn't provide Koenig lookup. – Rhys Ulerich May 13 '13 at 14:12
2

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.

billz
  • 44,644
  • 9
  • 83
  • 100
  • That provides the brevity within the constructor but it doesn't provide Koenig lookup. One could add a `using namespaceA::namespaceB::etc;` line above the `tada(c)` bit to accomplish what I'm after. Thank you. – Rhys Ulerich May 13 '13 at 14:13
1

As a Very Gross Hack, one thing you could do would be to have two separate implementation files. One of the implementation files would be a normal implementation file in which you'd implement everything except the constructor. In the second implementation file, you'd do

#include "appropriate-header-file"
using namespace something_long;

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

That way, when compiling the constructor, the compiler will have the using declaration to make tada shorter. However, this doesn't pollute the namespace in the standard implementation file.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Maybe wrap `something_long::tada(c)` in a new function? like static private function. – billz May 08 '13 at 03:24
  • Clever, but I'm not sure the extra effort of creating a separate header file really beats out having to type out a namespace. – Yuushi May 08 '13 at 03:25
  • @Yuushi- This is just a proof of concept that shows that in principle it is possible to do this. Though I completely agree! – templatetypedef May 08 '13 at 03:28
  • Eewww.. That's a Very Gross Hack, but it does turn out that I can do something similar in my code that's Not Quite So Gross. Thank you. – Rhys Ulerich May 13 '13 at 14:14