0

this is a technical question as well as (if it's technically feasible) a style question. In my code I have a series of functions that load and manipulate a file. For different syntax these functions exist in separate namespaces, so that:

namespace typeA {
  void load();
  void manipulate();
};

namespace typeB {
  void load();
  void manipulate();
};

In this minimal example, obviously I could simple have a clause that checks for the file type and then calls typeA:load() and typeB:load() etc. as required.

However, I was wondering if it is also possible to do this using namespace aliases? It would make the code cleaner as there are several dozen calls to functions in the namespaces which all would need a separate if/else clause.

What I've tried:

namespace my_type = (ft == "type_A") ? typeA : typeB;
...
my_type::load();

which doesn't compile with an error in the namespace alias assignment line.

Is there another way of doing this / what is the generally accepted clean way of handling situations like this?

I suppose a virtual class and inheritance for each file type would be one option. Are there others?

kmh
  • 391
  • 3
  • 13
  • namespace declares the use of the next string that comes, how would it work that way? – Ariel Pinchover Apr 18 '13 at 14:12
  • you could also use templates... – Nim Apr 18 '13 at 14:13
  • @Nim: Thanks for the suggestion. How would I use templates in this situation? The inputs to the function would be the same only the contents of the file that is being passed by string would be different. – kmh Apr 18 '13 at 14:18

1 Answers1

2

There is no way to do such namespace alias.

3.4.6 Using-directives and namespace aliases [basic.lookup.udir] 1 In a using-directive or namespace-alias-definition, during the lookup for a namespace-name or for a name in a nested-name-specifier only namespace names are considered.

You an use partial template specialization to solve that.

Community
  • 1
  • 1
alexrider
  • 4,449
  • 1
  • 17
  • 27
  • Thanks for the pointer. I think I figured out how to do this now, by creating a struct across all my functions and then custom types to specify which implementation I'm interested in. Is this considered good style? – kmh Apr 18 '13 at 14:55
  • @Martin Not sure if I correctly understood both your goal and solution, thru I'd suggest you checking boost::function and boost::bind(std::function and std::bind in c++11). – alexrider Apr 18 '13 at 15:00
  • @Martin A possible solution to this is by creating a traits struct. Now specialize it for different tag types. For examples see how the standard library dispatches algorithms based upon iterator tags. See the sample on [msdn](http://msdn.microsoft.com/en-us/library/zdxb97eh(v=vs.71).aspx) – indeterminately sequenced Apr 18 '13 at 15:13
  • @Martin Yes this could be solution, but only if it is known at compile which tag type will be used, since template instantiation happens during compilation. Otherwise you still need decide which tag type instantiated struct to use at runtime. – alexrider Apr 18 '13 at 15:20