English 中文(简体)
印刷图谱操作员
原标题:Templatize stream operator for printing enumerations

我有一个名称空间,通过我的申请,加上相关的印刷声明,创造了一系列的词汇。

namespace NS {
    enum EnumA { A1, A2, A3 };
    enum EnumB { B1, B2, B3 };

    inline std::string toString(const EnumA key) {
        switch(key) {
        case A1: return "A1";
        case A2: return "A2";
        case A3: return "A3";
        }
        return "UNKNOWN";
    }

    // .. same for EnumB
}

// this is the part I would like to templatize
inline std::ostream& operator<<(std::ostream& s, const NS::EnumA key) {
    s << NS::toString(key);
    return s;
}
inline std::ostream& operator<<(std::ostream& s, const NS::EnumB key) {
    s << NS::toString(key);
    return s;
}

有可能使下游经营者与任何<代码>NS合作 我只得这样说? 和:

template <typename T>
inline std::ostream& operator<<(std::ostream& s, const NS::<T> key) {
    s << NS::toString(key);
    return s;
}
最佳回答

如果名称空间中的唯一东西(NS)属于可计量的类型,你可将operator<<列入名称空间,因此:

namespace NS {
  enum EnumA { A1, A2, A3 };
  enum EnumB { B1, B2, B3 };

  inline std::string toString(const EnumA key) {
    ...
  }
  inline std::string toString(const EnumB key) {
    ...
  }

  template <typename T>
  inline std::ostream& operator<<(std::ostream& s, const T key) {
    std::operator << (s, NS::toString(key));
    return s;
  }

}

A complete program is here.

问题回答

The obvious approach will result in your overload being considered for all types; this will result in calls being ambiguous for all types, since templates are not choosy. So you need some way to tell the compiler that your enum is the right type of enum, and to ignore all the rest; a traits class is probably the easiest way to do that.

namespace NS {
  enum EnumA { };

  template<typename T>
  struct is_ns_enum : std::false_type { };

  template<>
  struct is_ns_enum<EnumA> : std::true_type { };
}

From there, you can use SFINAE to implement your function.

template<typename T>
inline typename std::enable_if<is_ns_enum<T>::value, std::ostream&>::type
operator <<(std::ostream& s, const T&) {
  ...
}

这样,如果你专门使用<代码>is_ns_enum,则考虑超载,但被弃置于其他所有类型,从而防止这种高压错误。

如果您对流层类型也有所节制,这将发挥作用:

#include <string>
#include <iostream>
using namespace std;

namespace NS {
    enum EnumA { A1, A2, A3 };
    enum EnumB { B1, B2, B3 };

inline std::string toString(const EnumA key) {
        switch(key) {
        case A1: return "A1";
        case A2: return "A2";
        case A3: return "A3";
        }
        return "UNKNOWN";
    }

inline std::string toString(const EnumB key) {
        switch(key) {
        case B1: return "B1";
        case B2: return "B2";
        case B3: return "B3";
        }
        return "UNKNOWN";
    }
};

template <class Stream, typename T>
inline std::ostream& operator<<(Stream& s, T key) {
    s << NS::toString(key);
    return s;
}

int main()
{
    cout << NS::A2;
    cout << NS::B3;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签