マイクロコントローラー用の C++ ライブラリに取り組んでおり、テンプレートの種類に基づいて異なる列挙型クラスのコンテンツを指定する方法が必要です。
私はこれまでにこのコードを持っています:
#include <cstdint>
#include <type_traits>
enum class timer_e {
timer1,
timer2
};
template<typename>
struct is_basic_timer : std::false_type {};
template<>
struct is_basic_timer<timer_e::timer1> : std::true_type {};
template<typename>
struct is_advanced_timer : std::false_type {};
template<>
struct is_advanced_timer<timer_e::timer2> : std::true_type {};
template<timer_e T>
class Timer {
public:
// Prototype for options enum.
enum class options;
typename std::enable_if<is_basic_timer<T>::value, decltype(options)>::type options : std::uint32_t {
basic_option_1,
basic_option_2
};
typename std::enable_if<is_advanced_timer<T>::value, decltype(options)>::type options : std::uint32_t {
basic_option1,
basic_option2,
advanced_option1,
advanced_option2
};
static void enableOption(options o) {
SFR |= static_cast<std::uint32_t> (o);
}
static void disableOptions(options o) {
SFR &= ~static_cast<std::uint32_t> (o);
}
private:
Timer() = delete;
};
Timer クラスは、静的にのみ使用されることになっています。サポートされていないオプションが特殊機能レジスタ (SFR) に設定されるのを避けるために、テンプレート タイプに基づいてオプション列挙の内容を変更したいと考えています。上記のコードは私の最善の試みですが、コンパイラは decltype の使用を好みません。
上記で定義した型特性を介して、テンプレート型に基づいて異なる列挙型クラスのコンテンツを宣言する方法はありますか?