使用Sfinae检查基于模板中的成员是否存在
示例在这里关于是否存在给定类型的成员。我想编写一个基于模板为类工作的元功能。如果在具有所需成员的功能调用中提供了基于模板的类,则应成功。
课程:
template <typename KeyType>
struct RawBytesEntry
{
bool used = false;
size_t psl = 0;
KeyType key;
...
}
我尝试了许多不同的建议和语法排列,但我无法正常工作。有人有任何建议吗?我认为这是我得到的“最接近”:
template <template <typename> typename EntryType, typename = void>
struct has_psl : std::false_type
{
};
template <template <typename KeyType> typename EntryType>
struct has_psl<EntryType<KeyType>, std::void_t<decltype(EntryType<KeyType>::psl)>> : std::true_type
{
};
template <template <typename KeyType> typename EntryType, std::enable_if<has_psl<EntryType>::value>>
bool f(EntryType<KeyType> *buffer, size_t offset, EntryType<KeyType> *new_entry)
The example here shows how we can use TMP to have the compiler elide functions based on whether a member exists for a given type. I want to write a metafunction that works for classes based on templates. Compilation should succeed if a class based on a template is provided in the function call that has the desired member.
The class:
template <typename KeyType>
struct RawBytesEntry
{
bool used = false;
size_t psl = 0;
KeyType key;
...
}
I tried a whole slew of different suggestions and syntax permutations but I couldn't get it to work. Does anyone have any suggestions? Here is what I think is the "closest" I got:
template <template <typename> typename EntryType, typename = void>
struct has_psl : std::false_type
{
};
template <template <typename KeyType> typename EntryType>
struct has_psl<EntryType<KeyType>, std::void_t<decltype(EntryType<KeyType>::psl)>> : std::true_type
{
};
template <template <typename KeyType> typename EntryType, std::enable_if<has_psl<EntryType>::value>>
bool f(EntryType<KeyType> *buffer, size_t offset, EntryType<KeyType> *new_entry)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模板参数 模板模板参数无法在身体中使用。
然后,
在线演示
The template parameters of the template template parameter can not be used in the body.
And then,
Online Demo