问题描述
template <class T,class Enum,size_t N>
struct enum_array {
template <Enum E>
void do_fancy_thing_with_non_type();
};
我在实现 get 函数(它在单独的命名空间中)时遇到问题,因为提供的非类型模板参数取决于推导的类模板参数。
// This cannot work,'Enum' isn't deduced at point of declaration.
template <Enum E,class T,size_t N>
constexpr T& get(enum_array<T,Enum,N>& a) noexcept {
//return ...;
}
get<some_enum::val>(arr);
我知道 c++17 有 auto
模板参数可以解决这个问题,但不幸的是,这段代码只是 c++14。有没有办法推导出非类型参数类型,同时仍然要求用户在 c++14 中提供它?
解决方法
这似乎在 c++14 中是不可能的。最好的办法是使用成员函数。