可以将模板字段参考模板参数作为第一个模板参数传递
Can templated field reference template parameters be passed as the first template parameter?
我在 template <class Base> class foo {}
中具有以下方法:
template <
typename V,
template <class, class> class Map,
Map<std::string, V> Base::*Field,
std::size_t Len
>
constexpr const foo<Base> s(char const (§ion_name)[Len]) const {
#pragma unused(section_name)
return *this;
}
我的目标是能够简单地称呼它:
struct my_struct {
std::map<std::string, int> section;
constexpr auto bar = foo<my_struct>()
.s<&my_struct::section>("hi");
};
也许是通过专业化?我无法围绕如何使此内容进行编译,因为目前我无法通过指针到成员,因为模板的地图类型在声明中首先出现,这使我在Clang上获得了以下内容:
注意:忽略候选模板:模板参数'map'
的无效明确指定参数
,我似乎也无法获得专业的神奇咒语:
template <auto FieldType, std::size_t Len>
constexpr const foo<Base> s(char const (§ion_name)[Len]) const;
template <
template <class, class> class Map,
typename SecType,
Map<std::string, SecType> Base::*Field,
std::size_t Len
>
constexpr const foo<Base> s<Field, Len>(char const (§ion_name)[Len]) const {
}
给出
错误:功能模板部分专业不允许
我还可以吗?值得一提的是C 14及以前的是可以的。
对于Pre-C 17解决方案,您可以将成员指针放入std::integral_constant
。要确保地图的关键类型是std::string
,请使用类型扣除来从指针获取成员类型,并将其密钥类型与std::string
进行比较:
template<class T, class C>
T get_member_type(T C::*);
template<class Base>
struct foo{
template<class I, std::size_t Len>
constexpr const foo<Base> s(I, char const (§ion_name)[Len]) const {
using M=decltype(get_member_type(I::value));
static_assert(std::is_same<typename M::key_type, std::string>{});
return *this;
}
};
#define CONST(e) std::integral_constant<decltype(e), e>
struct my_struct {
std::map<std::string, int> section;
constexpr static auto bar = foo<my_struct>().s(CONST(&my_struct::section){}, "hi");
};
您在编辑中遇到的错误是由于您无法部分专业化功能模板。幸运的是,您可以在上面的答案中扩展代码,以便您可以根据键类型过载标签:
:template<class Base>
struct foo{
template<class M>
using get_key_type = typename M::key_type;
template<class I>
using get_member_type = decltype(::get_member_type(I::value));
template<class I, std::size_t Len>
constexpr auto s(I, char const (§ion_name)[Len]) const {
return s(I{}, section_name,
std::is_same<get_key_type<get_member_type<I>>, std::string>{}
);
}
private:
template<class I, std::size_t Len>
constexpr const auto s(I, char const (§ion_name)[Len], std::false_type) const {
return *this;
}
template<class I, std::size_t Len>
constexpr const auto s(I, char const (§ion_name)[Len], std::true_type) const {
return *this;
}
};
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何将部分流作为参数传递
- 通过常量引用传递参数的矩阵模板类
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- c++构造函数成员初始化:传递参数
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用