Alex Vergara
Alex Vergara

Reputation: 2258

Defining the size of an array on a custom templated Array class

I am making a project to write from scratch several datastructures, procedures, probably a mini-testing framework... things already well known and coded, but just with the purpose of learn and to adquire a much more deeper knowledge of the fundamentals of programming, but in a modern way, or, at least, using the modern tools of the C++ language.

So, there's the repo for the code. Zero. The code provided there is currently working under Unix systems, with clang >= 14. Also, the repo contains a tool called Zork, that is a custom C++ project builder focused on build C++ project with the C++20 modules feature. On the root folder of the project, you will find a zork++ file, that is the compiled binary for execute the tool. So, with just ./zork++ the tool will compile and link the project. That's an entry point as an ultra basic testing tool, that is just the main.cpp file calling the things and kind of testing it's funcionalities.

There's a zork.conf file, that is the configuration file to set up the commands that will be sent to the compiler to automate the project.

This spoiler is for provide the real code behind the question. I plan to make several questions on Stack Overflow for this project, so this one is setted as a base explanation for the "code example" required usually.

After this, let's go with the real question.

I started for make collections. Custom ones. So, for me, the very basic one (things should be the easy first ones, and then code to the most difficult ones in difficulty ascending order) is to simulate the C++ Array class.

This is the snippet:

export namespace zero::collections {
    template <typename T, unsigned long>
    class Array {
        private:
            T array[];
    };
}

First doubt is about the second parameter of the templated class. Second parameter must provide the desired initial capacity of the array member. I just choose long because is long enought to store any signed number (I guess that is too much...). I don't know the formal way in C++ of define this parameters that have real sense with the implementation.

A simple int would fit for most common cases. But I want to make it perfectly generic. I thought about constraint it with a concept. The idea is that parameter, must be an unsiged natural number. But not specifically a short, and int or a long. Something like number, for example. But I can't figure out the best way of code the requirement.

What could be the formal way of defining this requirement in modern C++, and use that template parameter to const initialize the array with a fixed size?

Upvotes: 1

Views: 111

Answers (1)

Pylyv
Pylyv

Reputation: 56

Your are looking for the std::size_t, which can be found on some system headers. Extracted from cppreference:

  • Defined in header cstddef
  • Defined in header cstdio
  • Defined in header cstdlib
  • Defined in header cstring
  • Defined in header ctime
  • Defined in header cuchar
  • Defined in header cwchar (since C++17)

Basically, you should include one of them, and use std::size_t for the template parameter that must define the amount of elements that the user wants to store in the array.

export module your_module;

#include <cstddef>

export namespace zero::collections {
    template <typename T, std::size_t N>
    class Array {
        private:
            T array[N];
    };
}

Also note that you must define the size of the array member with the type parameter N.

Upvotes: 3

Related Questions