gpt4 book ai didi

c++ - 错误 : 'int_type' does not name a type - How to inherit typedefs and usings

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:52 25 4
gpt4 key购买 nike

我正在编写从 std::basic_streambuf 派生的层 streambuf:

/// Basic Socket Buffer
template<
typename CharT_,
typename Traits_ = std::char_traits< CharT_ > >
class BasicSocketBuffer //BasicSocketStreamBuffer
: public std::basic_streambuf< CharT_, Traits_ >
{
...
virtual int_type
overflow(int_type __c = traits_type::eof());
...

错误:

error: 'int_type' does not name a type
virtual int_type
^~~~~~~~
note: (perhaps 'typename std::basic_streambuf<_CharT, _Traits>::int_type' was intended)

哈哈。编译器知道我想要什么,但不会做。为什么?

这里是父标准类的第一行:

template<typename _CharT, typename _Traits>
class basic_streambuf
{
public:
//@{
/**
* These are standard types. They permit a standardized way of
* referring to names of (or names dependent on) the template
* parameters, which are specific to the implementation.
*/
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
...

解决方法

几乎在派生类中再次重新声明所有使用的类型;

class Derived : class Parent {
using Parent::int_type; // for non-templated
using typename Parent::char_type;
...

你知道更好的方法吗?欢迎使用 C++11,14。

最佳答案

Ha-ha. Compiler knows what I want, but won't do. Why?

C++ 标准规定您必须在那里使用 typename 才能使代码有效,因为类模板中的名称查找不会查找依赖基。参见 https://gcc.gnu.org/wiki/VerboseDiagnostics#dependent_base及其链接的解释。

Do you know better way?

这才是正确的做法。

关于c++ - 错误 : 'int_type' does not name a type - How to inherit typedefs and usings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44654956/

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