Pointers and Arrays

Pages: 12
closed account (o1vk4iN6)
Not a pointer, it's an array. Why wouldn't you ?

1
2
3
4
5
6
7
int main()
{
    std::unique_ptr<float[]> p(new float[1000]);

    // no need to call delete[]
    return 0;
}
I don't think that's how it works...
That is how it works.
http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_at



If you can live with an altered syntax for (logical) member access:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

template < std::size_t POS > struct get_array_element
{
    template < typename T >
    auto operator() ( T&& t ) const -> decltype( *( std::forward<T>(t) ).array )
    { return std::forward<T>(t).array[POS] ; }
};

struct vertex { float array [5] ; } ;

constexpr auto x = get_array_element<0>() ;
constexpr auto y = get_array_element<1>() ;
constexpr auto z = get_array_element<2>() ;
constexpr auto u = get_array_element<3>() ;
constexpr auto v = get_array_element<4>() ;

int main ()
{
    vertex vert { { 1.2, 3.4, 5.6, 7.8, 9.1 } } ;
    y(vert) += 0.5 ;
    std::cout << x(vert) << ", " << y(vert) << ", " << z(vert)
              << ", " << u(vert) << ", " << v(vert) << '\n' ;
}

http://ideone.com/eEkf5m
I see, so it specializes on T * and treats it like an array? This is a little unintuitive to me for some reason.
closed account (o1vk4iN6)
How many times does it have to be said, arrays are not pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <typename T>
class V
{
public:

    static const int a = 10;
};

template <typename T>
class V<T[]>
{
public:
    static const int a = 20;

};

template<typename T>
class V<T*>
{
public:
    static const int a = 30;
};

int main()
{
    // outputs 102030
    std::cout << V<float>::a << V<float[]>::a << V<float*>::a << std::endl;

    return 0;
}
Last edited on
Unbelievable - I had no idea that T[] wasn't equal to T* (I'm not kidding, I actually thought not specifying a size for the array degraded it to a pointer).

Mainly because of this: http://ideone.com/Wifu2z

It's inconsistent and confusing :\
closed account (o1vk4iN6)
http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

The naming convention used by typeinfo is compiler specific. So yah zero or no length arrays are not defined by C++ and are simply extensions provided by some compilers. Kind of surprised gcc doesn't give a warning about that.
I was more concerned about the declaration with no number inside the square brackets, but OK.
T[] in a function parameter list is converted to T* (same for function types), because neither arrays nor functions can be passed by value to begin with.

T[] in template parameter list is not converted (and neither are function types), because they are distinct types.

A declaration of a member of a struct is a whole other story about non-standard language extensions. gcc reports it with -pedantic-errors
Last edited on
Cubbi wrote:
T[] in a function parameter list is converted to T* (same for function types), because neither arrays nor functions can be passed by value to begin with.
This is what I was thinking of, I understood this to be true in all cases, not just a function parameter list. Misunderstanding on my part.
Why not, as someone suggested, just have it as an array? Then you can provide methods to access it with x/y syntax, like so:
1
2
3
4
5
6
struct Point {
    float data[2];
    Point(float x, float y) { data[0] = x; data [1] = y; }
    float& x() { return data[0]; }
    float& y() { return data[1]; }
};
Last edited on
array with x()/y() syntax is what boost uses for 2D points
Topic archived. No new replies allowed.
Pages: 12