StackPointer template
StackPointer template
本文关键字:template StackPointer 更新时间:2023-10-16
我正在阅读"现代c++设计",并突然想到构建一个类,它将像指针一样工作,但它将在堆栈而不是堆上分配对象。它可用于通常返回指向堆上分配的对象的指针的函数。
在粘贴代码之前我会问我的问题:
- 已经有类似的东西了吗?
- 有机会被使用吗?(当然,如果实现得更准确的话)
- 为什么版本使用
boost::mpl::max_element
(注释掉)不工作? - 如何调用模板构造函数,如果它没有参数?(我的意思是:
template <class U> StackPointer() { ... }
)?
代码如下:
#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/max_element.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/front.hpp>
template <class V, size_t VS=boost::mpl::size<V>::type::value>
struct max_size
{
typedef typename boost::mpl::pop_front<V>::type subvector;
typedef typename boost::mpl::front<V>::type front_type;
static size_t const value = sizeof(front_type) > max_size<subvector>::value ?
sizeof(front_type) : max_size<subvector>::value;
};
template <class V>
struct max_size<V, 0>
{
static size_t const value = 0;
};
class StackPointerImplBase
{
public:
virtual void clone(char const* from, char* to) const = 0;
};
template <class T>
class StackPointerImpl : public StackPointerImplBase
{
public:
virtual void clone(char const* from, char *to) const
{
new(to) T(*reinterpret_cast<T const*>(from));
}
};
template <class Base, class DerivedTypes>
class StackPointer
{
public:
template <class T>
StackPointer(T const& t)
{
std::cout << "Size of m_buf: " << sizeof(m_buf) << std::endl;
new(m_impl_buf) StackPointerImpl<T>();
new(m_buf) T(t);
}
StackPointer(StackPointer const& sp)
{
//TODO: COPY m_impl_buf
reinterpret_cast<StackPointerImplBase const*>(sp.m_impl_buf)->clone(sp.m_buf, m_buf);
}
public:
~StackPointer()
{
get_pointer()->~Base();
}
Base* operator->()
{
return get_pointer();
}
private:
Base* get_pointer()
{
return reinterpret_cast<Base*>(m_buf);
}
private:
//typedef max_size<DerivedTypes> xxx_type;
//typedef typename boost::mpl::max_element<DerivedTypes>::type::type biggest_type;
//char m_buf[sizeof(typename boost::mpl::max_element<DerivedTypes>::type::type)];
char m_buf[max_size<DerivedTypes>::value];
char m_impl_buf[sizeof(StackPointerImplBase)];
};
class Shape
{
public:
virtual ~Shape() {}
virtual void say() const { std::cout << "I'm a shape" << std::endl; }
};
class Circle : public Shape
{
public:
virtual void say() const { std::cout << "I'm a circle" << std::endl; }
private:
float m_x;
float m_y;
float m_r;
};
class Line : public Shape
{
public:
virtual void say() const { std::cout << "I'm a Line" << std::endl; }
private:
float m_x1;
float m_y1;
float m_x2;
float m_y2;
};
typedef StackPointer<Shape, boost::mpl::vector<Circle, Line> > ShapeStackPtr;
ShapeStackPtr read_shape()
{
Line c;
return ShapeStackPtr(c);
}
int main(int argc, char *argv[])
{
{
ShapeStackPtr shape = read_shape();
shape->say();
}
return 0;
}
Boost optional与您所描述的非常相似。
它使用指针语法,所以它可以模拟指针语言中的NULL
指针。
你不能在堆栈上分配一个对象,然后返回指向它的任何东西。当你从一个函数返回后,堆栈帧已经消失,指向那个内存是无效的。
当你在函数中声明局部变量时,它们在堆栈上分配,然后在函数返回之前销毁,堆栈帧作废。
因此,您可以为堆上的对象分配内存并通过指针引用它们。或者在堆栈上的函数中有局部变量。
或者你的意思是作为一种方式来处理从调用函数返回值?但是,无论如何,您需要将数据从堆复制到堆栈,所以如果您需要/想要堆栈上的副本,为什么不使用T obj = *foo()
呢?
对我来说,这听起来像是你试图使编译器已经有效处理的事情复杂化。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 传递给std::function template的template参数究竟代表什么
- 在template中使用std::variant的template函数
- C++ template for QList
- C++ - 为什么这里需要'template'关键字?
- 使用"std::enable_if_t" "function template has already been defined"
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- C++ class template
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 为什么编译器说"candidate template ignored: couldn't infer template argument 'InputIterator'"?
- 为什么在这种情况下我需要 .template
- std::span constructor, libcxx vs libstdc++, template vs non-
- 模板函数参数到模板函数的"candidate template ignored: could not match ..."
- MacOS 上的 LLVM - 标准文件 iosfwd 中未知类型名称'template'
- std::get like (partial) template specialization
- 实现多类型算术运算符时如何解决"template argument deduction/substitution failure"
- 更新 Visual Studio 2017,现在出现编译错误 C7510:"回调":使用依赖模板名称必须以 'template' 为前缀
- C++ 中的"template <typename From, typename Tag> struct Alias;"是什么?
- Template类定义中的Template方法与声明不匹配
- 为什么在template函数广播中把两个extensor表达式加在一起不正确