Check if a type is from a particular namespace

前端 未结 3 1192
你的背包
你的背包 2020-12-02 18:37

I would like to check if a type is from a particular namespace. Here is what I came up with:

#include 

namespace helper
{
  template 

        
3条回答
  •  一向
    一向 (楼主)
    2020-12-02 19:15

    Unfortunately this technique works only for non-template types. For template types, ADL also checks the namespaces of the template arguments. Then it collects a list of class or function templates (depending on the context ADL is invoked from) and selects the best candidate.

    A better solution would be to add an explicit check to the types whose membership in a namespace you want to check. For example, you could derive all types from a certain class or add a special member to each of them. This would be much clearer, easier to understand and maintain solution.

提交回复
热议问题