gpt4 book ai didi

c++ - 无法从模板中提取 value_type

转载 作者:行者123 更新时间:2023-11-28 01:33:56 28 4
gpt4 key购买 nike

我正在尝试使用以下代码从容器中提取 value_type

//CustomTraits.h
template<typename C, typename = void>
struct has_push_back : std::false_type {};

template<typename C>
struct has_push_back<C, std::void_t< decltype(std::declval<C>())::value_type > > :
std::true_type {};

它是这样调用的

//MessaDecoder.h
template <typename Container,
typename = std::enable_if_t<has_push_back<Container>::value>
>
class MessageDecoder{/*Class Def...*/};

//Server.h
using buffer_t = std::deque<std::tuple<std::string, uint64_t>>;
std::shared_ptr<MessageHelper::MessageDecoder<buffer_t>> _decoder_ptr;

为了使这段代码能够编译,我尝试了很多变体。我已经通过使用 Container::value_type 将其提取为 MessageDecoder.h 中的模板参数来测试 value_type 是否有效,并且它确实可以编译。但是,执行相同的操作以及我在 CustomTraits.h 中所做的操作无法正确触发专用模板,我想知道为什么以及如何修复它。这是我遇到的一些错误。

Error   C3203   'MessageDecoder': unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type  

Error C2938 'enable_if_t<false,void>' : Failed to specialize alias template

我正在使用最新更新的 VS2017 C++ 17。

最佳答案

改变

decltype(std::declval<C>())::value_type

typename C::value_type

declval 给你一个右值引用,你不能把成员从引用类型中拉出来(而且这种舞蹈是不必要的,因为你只想要类型,你有:C)。而且您缺少 typename

关于c++ - 无法从模板中提取 value_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50242920/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com