use sfinae to test namespace members existence

不想你离开。 提交于 2019-11-30 15:01:44

No, that won't work. There is also no way to use SFINAE in such a way (this was last discussed on usenet for a compatibility test against some C++0x component). The C inside xyz::C is not related to the template parameter at all.

Remember that templates are not just macros. The parameter C denotes not just a piece of text, but a semantical entity. In this case it is a type. It's bound already to the meaning it has as an argument. That is, if your class had a member of name abc, the meaning of the parameter still would not change.

If all you want is to use some struct xyz::abc if it exists, and others::abc otherwise, you can do some tricks to get there, but I'm not aware of a way that does it without touching xyz

namespace others {
  struct abc{};
}

namespace fallbacks {
  using others::abc;
}

namespace xyz {
  using namespace fallbacks;
}

Now if you say xyz::abc and xyz contains a member declared so, it will refer to that member (that member will hide the one in fallbacks. However if it doesn't contain that member, then the using directive's name will be found and refer to fallbacks::abc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!