• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

C++ use std::enable_if to create std::tuple specialisations up to 10 arguments

April 10, 2020 by

Questions › C++ use std::enable_if to create std::tuple specialisations up to 10 arguments
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I want to create a tuple that has specializations for up to 10 args, similar to how std::pair is a specialization for two args.

i.e tuple<int,float,bool> will have the members first(), second(), and third()

Here is my attempt so far:

#pragma once

#include <tuple>
#include <type_traits>

template<typename... Types>
struct tuple : std::tuple<Types...> {

    using std::tuple<Types...>::tuple;
    static constexpr size_t size = sizeof...(Types);

    template<size_t N>
    using elem_n = std::tuple_element_t<N, std::tuple<Types...>>;

    template<size_t N>
    const elem_n<N>& get() const { return std::get<N>(*this); }

    template<size_t N>
    elem_n<N>& get() { return std::get<N>(*this); }

    template<bool F = false> std::enable_if_t< (size >= 1) || F, const elem_n<0>&> first() const { return get<0>(); }
    template<bool F = false> std::enable_if_t< (size >= 2) || F, const elem_n<1>&> second() const { return get<1>(); }
    template<bool F = false> std::enable_if_t< (size >= 3) || F, const elem_n<2>&> third() const { return get<2>(); }
    template<bool F = false> std::enable_if_t< (size >= 4) || F, const elem_n<3>&> fourth() const { return get<3>(); }
    template<bool F = false> std::enable_if_t< (size >= 5) || F, const elem_n<4>&> fith() const { return get<4>(); }
    template<bool F = false> std::enable_if_t< (size >= 6) || F, const elem_n<5>&> sixth() const { return get<5>(); }
    template<bool F = false> std::enable_if_t< (size >= 7) || F, const elem_n<6>&> seventh() const { return get<6>(); }
    template<bool F = false> std::enable_if_t< (size >= 8) || F, const elem_n<7>&> eighth() const { return get<7>(); }
    template<bool F = false> std::enable_if_t< (size >= 9) || F, const elem_n<8>&> ninth() const { return get<8>(); }
    template<bool F = false> std::enable_if_t< (size >= 10) || F, const elem_n<9>&> tenth() const { return get<9>(); }

    template<bool F = false> std::enable_if_t< (size >= 1) || F, elem_n<0>&> first() { return get<0>(); }
    template<bool F = false> std::enable_if_t< (size >= 2) || F, elem_n<1>&> second() { return get<1>(); }
    template<bool F = false> std::enable_if_t< (size >= 3) || F, elem_n<2>&> third() { return get<2>(); }
    template<bool F = false> std::enable_if_t< (size >= 4) || F, elem_n<3>&> fourth() { return get<3>(); }
    template<bool F = false> std::enable_if_t< (size >= 5) || F, elem_n<4>&> fith() { return get<4>(); }
    template<bool F = false> std::enable_if_t< (size >= 6) || F, elem_n<5>&> sixth() { return get<5>(); }
    template<bool F = false> std::enable_if_t< (size >= 7) || F, elem_n<6>&> seventh() { return get<6>(); }
    template<bool F = false> std::enable_if_t< (size >= 8) || F, elem_n<7>&> eighth() { return get<7>(); }
    template<bool F = false> std::enable_if_t< (size >= 9) || F, elem_n<8>&> ninth() { return get<8>(); }
    template<bool F = false> std::enable_if_t< (size >= 1) || F, elem_n<9>&> tenth() { return get<9>(); }

};

I have also tried it with:

template<size_t N>
using elem_n = std::conditional_t<(size >= N), std::tuple_element_t<N, std::tuple<Types...>>, void>;

But when testing with

using my_tripple = tuple<int, std::string, float>;
my_tripple a;

a.first() = 6;
a.second() = "hello";
a.third() = 0.1f;

I get the compile errors:

/usr/include/c++/9/tuple:1303: error: static assertion failed: tuple index is in range
 1303 |       static_assert(__i < tuple_size<tuple<>>::value,
      |                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

And from fourth() to tenth()

 error: no type named ‘type’ in ‘struct std::tuple_element<3, std::tuple<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >’
   26 |     template<bool F = false> std::enable_if_t< (size >= 4) || F, const elem_n<3>&> fourth() const { return get<3>(); }
      |                                                                                    ^~~~~~

This is related to the question c++ Use std::enable_if to conditionally add getters to a variadic variant template, but the solution from that doesn't work here.

Thanks

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, c++17, enable-if, template-meta-programming, tuples

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023