性病shared_pointer_cast未定义

std shared_pointer_cast not defined

本文关键字:未定义 cast pointer 性病 shared      更新时间:2023-10-16

我正在尝试一个在 apache 箭头C++示例中记下的示例示例。 该示例使用共享点转换,如下所示(代码片段)

版本

g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3aka8.0.2) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

法典

int main()
{
Int64Builder builder(arrow::default_memory_pool(), arrow::int64());
builder.Append(8);
std::shared_ptr<Array> array;
builder.Finish(&array);
std::shared_ptr<Int64Array> int64_array = std::shared_pointer_cast<Int64Array>(array);

return 0;
}

编译标志

但是,当我使用以下标志编译代码时,出现未定义的错误。

g++ example.cpp -O2 -std=c++11 -I/workspace/arrow/arrow-master/cpp/src  -L/workspace/arrow/arrow-master/cpp/release/release  -larrow -larrow_jemalloc  

错误

example.cpp: In function 'int main()':
example.cpp:24:44: error: 'shared_pointer_cast' is not a member of 'std'
std::shared_ptr<Int64Array> int64_array = std::shared_pointer_cast<Int64Array>(array);
^
example.cpp:24:79: error: expected primary-expression before '>' token
std::shared_ptr<Int64Array> int64_array = std::shared_pointer_cast<Int64Array>(array);

我没有看到任何std::shared_pointer_cast的文档

问题

  1. 我错过了什么,这是 c++11 版本中的可编译代码吗?
  2. C++11 版本中的替代方案是什么?

您应该根据您的意图使用static_pointer_castdynamic_pointer_cast

以下是 C++ 11 中支持的所有转换:

http://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast

但是,在 C++11 中没有什么叫做std::shared_pointer_cast

此外,static_cast应该适合您。尝试static_cast而不是std::shared_pointer_cast

使用强制转换时要考虑的要点:

static_cast不执行运行时检查。如果您知道引用特定类型的对象,则应使用此方法,因此不需要检查。它也用于避免隐式转换,而是使其明确

dynamic_cast用于您不知道对象的动态类型的情况。如果向下转换并且参数类型不是多态的,则不能使用dynamic_cast。举个例子:

Regular cast是一个C式演员阵容,结合了所有的const_caststatic_castreinterpret_cast,但它也是不安全的。

查看此答案以了解有关C++中使用的铸件的更多信息。