On 07/30/2018 11:18 PM, Stefan Ram wrote:
> So, is »decltype( a )::value_type« here the most elegant /
> natural / concise way to express that type or is there a
> better way? (Since this is intended to show off »value_type«,
> »value_type« should appear in the answer. So, »auto v = ...«
> would not be helpful.)
things like value_type are most useful with template (meta)programming,
so it could be useful to show its use with template type arguments,
instead of decltype:
#include <array>
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
using namespace ::std::literals;
template<typename Container>
void PrintFirstOf(const Container& c)
{
typename Container::value_type v = std::get<0>(c);
std::cout << v << std::endl;
}
int main()
{
{
::std::array const a{ 4, 2, 7 };
PrintFirstOf(a);
// decltype( a )::value_type v = ::std::get< 0 >( a );
// ::std::cout << v << '\n';
}
{
::std::array const a{ "alpha"s, "gamma"s, "delta"s };
PrintFirstOf(a);
// decltype( a )::value_type v = ::std::get< 0 >( a );
// ::std::cout << v << '\n';
}
}
As a side effect it would be probably good to add here some assurance
that the size oft he array is > 0