Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to get std::array's size at compile time
QtWS25 Last Chance

How to get std::array's size at compile time

Scheduled Pinned Locked Moved Solved C++ Gurus
c++constexpr
4 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 17 Jan 2020, 09:42 last edited by kshegunov
    #1

    Hello,
    I have a function which returns a std::array and I want to have some logic based on the size of the array at compile time. Is there a way to get the size of the array off the return value of my function. Code looks something like this:

    auto someFunction()
    {
        return std::array<double, 14>{ 0.1, ..., ...};
    }
    

    And I want to plug it into a if constexpr to branch out a specific case. Something akin to this:

    constexpr decltype(someFunction().size()) size = ...;
    if constexpr (size > 10)  {
       // Do special stuff
    }
    

    Is this possible at all?

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 17 Jan 2020, 10:01 last edited by VRonin
      #2

      I's very easy, you just use std::tuple_size. The naming is misleading but it does its job

      #include <iostream>
      #include <array>
      #include <type_traits>
      
      auto someFunction(){
          return std::array<double,3>{1.1,2.2,3.3};
      }
      
      int main()
      {
          if constexpr (std::tuple_size<std::result_of<decltype(someFunction)&()>::type>::value > 1)  
              std::cout<<"More than 1";
          return 0;
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      7
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 17 Jan 2020, 10:38 last edited by mrjj
        #3

        Hi
        Or like this which also seems to work

        #include <array>
        
        void Nurse();
        void Nuke();
        
        constexpr auto someFunction() {
            return std::array<double, 14>{ 0.1};
        }
        
        void test() {
        constexpr  decltype( someFunction().size()  ) size = someFunction().size();
        if constexpr (size > 10)  {
           Nuke();
        } else {
           Nurse();
          }
        }
        
        

        see here
        https://godbolt.org/z/Ktt36G

        But @VRonin version seems nicer :) \o/

        1 Reply Last reply
        8
        • K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 17 Jan 2020, 17:10 last edited by
          #4

          Thanks for the input guys, much appreciated!

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1

          3/4

          17 Jan 2020, 10:38

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved