2

I am trying a sample example noted down in apache arrow C++ example. The example uses shared pointed cast as below (code snippet)

Version

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.

Code

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;
}

Compilation Flags

However, when I compile the code with below flags, I get undefined error.

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  

Error

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);

I do not see any documentation for std::shared_pointer_cast

Question

  1. Am I missing something, is this compilable code in c++11 version?
  2. What are the alternative in C++11 version for this?
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
  • 1
    The example likely meant [`std::static_pointer_cast`](http://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast) – Igor Tandetnik May 27 '17 at 13:57
  • 1
    http://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast – songyuanyao May 27 '17 at 13:57
  • @songyuanyao: The link quoted does not have reference to shared_pointer_cast. Note that the example does not quote static_pointer_cast. – kumar_m_kiran May 27 '17 at 14:10
  • 2
    @kumar_m_kiran There's no such thing of `std::shared_pointer_cast`; the example is wrong..? Or it's just an abstract wording, which includes `std::static_pointer_cast`, `std::dynamic_pointer_cast`, `std::const_pointer_cast`, `std::reinterpret_pointer_cast`. – songyuanyao May 27 '17 at 14:13

1 Answers1

3

You should be using static_pointer_cast or a dynamic_pointer_cast based on what your intention is.

Here are all the casts supported in C++ 11:

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

But, there is nothing called a std::shared_pointer_cast in C++11

Additionally static_cast should work for you. Try static_cast instead of the std::shared_pointer_cast

Important points to consider when using casts:

static_cast performs no runtime checks. This should be used if you know that you refer to an object of a specific type, and thus a check would be unnecessary. It is also used to avoid the implicit conversion & rather make it explicit

dynamic_cast is used for cases where you don't know what the dynamic type of the object is. You cannot use dynamic_cast if you downcast and the argument type is not polymorphic. An example:

Regular cast is a C-Style cast which combines all of const_cast, static_cast and reinterpret_cast, but it's also unsafe.

Look at this answer to understand more about casts used in C++.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159