小编Jos*_*eph的帖子

有没有办法为C++ 03实现auto关键字

在C++ 03或更早版本中,有没有办法实现auto关键字?不是对象类,但它可以像这样使用[C++ 11]

auto x = 5;
std::cout << x;
Run Code Online (Sandbox Code Playgroud)

我很快就'掀起'了一个实现,但它很垃圾,因为你可以将它转换为任何类型 - 太像一个object类,而且非常基本,我知道,但无论如何,这里是:

class auto_t
{
public:
    template < typename _Ty > auto_t(const _Ty &_Value)
        : __data(_Value)
    {
    }

    template < typename _Ty >  operator _Ty()
    {
        return (_Ty)__data;
    }
private:
    void *__data;
};

#define auto auto_t
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
474
查看次数

为什么std :: allocator <> :: deallocate()有一个未使用的size_type参数?

使用时std::allocator,该deallocate函数需要一个pointer参数和一个size_type参数(std::allocator<>::deallocate(std::allocator<>::pointer p, std::allocator<>::size_type).但是,size_type不使用,也不是可选的.那么为什么会出现?它真的让我困惑,因为它应该是可选的,甚至不是那里,因为它没有在函数中使用.

编辑:MSVC的allocator实现deallocate

void deallocate(pointer _Ptr, size_type)
    {   // deallocate object at _Ptr, ignore size
    ::operator delete(_Ptr);
    }
Run Code Online (Sandbox Code Playgroud)

c++ parameters allocator

5
推荐指数
1
解决办法
415
查看次数

strnlen如何在C++中工作?

我很困惑如何strnlen使用非空终止的字符串在C++中工作,因为我不确定它如何计算大小.一个strlen实现很简单:

size_t strlen(char *s)
{
    size_t sz;
    while(*s++ != '\0')
    {
        ++sz;
    }
    return sz;
}
Run Code Online (Sandbox Code Playgroud)

但是你将如何实施strnlen()

c++ string

1
推荐指数
2
解决办法
7104
查看次数

C++ - std :: string :: ~string()字符串仍然可以打印为空值

当我和std::string我一起去的时候,我决定做以下事情:

int main(int argc, char **argv)
{
    std::string s = "Hello World";
    s.~basic_string();
    std::cout << s.c_str();
}
Run Code Online (Sandbox Code Playgroud)

但是,它什么都不打印,没有垃圾.但是,在我的 basic_string课堂上,当调用析构函数时,我会得到垃圾.怎么std::string办呢?我使用an allocatorconstructs,destroys,allocates和deallocates,但它仍然不起作用.

注意:我不是在寻找班级的解决方案,而是要了解std::string它是如何做到的.

c++ string

-1
推荐指数
1
解决办法
98
查看次数

标签 统计

c++ ×4

string ×2

allocator ×1

parameters ×1