I can't get the type of an element. This solution returns a reference to element type.
int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = decltype(*arr);
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));
I can't get the type of an element. This solution returns a reference to element type.
int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = decltype(*arr);
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));
Try the following
using arrElemType = std::remove_reference<decltype( *arr )>::type;
or
typedef std::remove_reference<decltype( *arr )>::type arrElemType;
You need to include header <type_traits>
std::decay
if I plan on storing it. –
Trafficator The standard way in C++11 and above is to use std::remove_all_extents
.
#include <type_traits>
int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = std::remove_all_extents<decltype(arr)>::type;
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));
Try the following
using arrElemType = std::remove_reference<decltype( *arr )>::type;
or
typedef std::remove_reference<decltype( *arr )>::type arrElemType;
You need to include header <type_traits>
std::decay
if I plan on storing it. –
Trafficator Since C++ 11, when only array type is given (not the array variable):
#include <type_traits>
template<typename T>
using array_entry_t = typename std::remove_reference<decltype( std::declval<T>()[0] )>::type;
Full example:
#include <iostream>
#include <typeinfo>
#include <type_traits>
template<typename T>
using array_entry_t = typename std::remove_reference<decltype( std::declval<T>()[0] )>::type;
int main()
{
using MyArr = char[3];
std::cout<< typeid( MyArr ).name() << std::endl; // Prints e.g: A3_c
std::cout<< typeid(array_entry_t<MyArr>).name() << std::endl; // Prints e.g: c
return 0;
}
My aim here is to give it a go without type_traits. With C++20, we can have lambda in decltype()
, so this code works with GCC10.2 flag -std=c++20
int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = decltype([&](){return arr[0];}());
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));
The below attempt was my first answer which got negative feedback. I kept it for educational purposes so others don't fall into this because of implicit integral type promotion:
uint8_t a;
auto x = +a; // x is int, not uint8_t
auto y = a+a; // y is int, not uint8_t
So while the example below works, if we change the type of array to uint8_t
it will still create vector<int>
.
OK, now read it:
If +
operator is defined for elements:
int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = decltype(+arr[0]); // + is the key
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));
<type_traits>
header is not needed.+arr[0]
expression turns to prvalue, which decltype deduces as T.decltype(arr[0])
gives T& which is not desirable here.arr[1000]
.+
is likely to convert the type to int
which is probably undesirable (it could be an array of uint8_t
). –
Portia © 2022 - 2024 — McMap. All rights reserved.
typeid
msdn.microsoft.com/en-us/library/fyf39xec%28v=vs.110%29.aspx – Rettkedecltype
. – Wiring