How to pass constexpr string array as parameter?


who I am

I have a file that contains an array of strings representing some icons.

static constexpr char icons1[2][40] = {
  "icon1_A", "icon1_B"
};

static constexpr char icons2[3][30] = {
  "icon22_A", "icon2_B", "icons2_C"
};

Then I have a class that I want to initialize by referencing these icons.

class Display {
public:
  Display(const char ** _icons) : icons(_icons), current_icon(0) {}

  void print_next_icon() {
    std::cout << icons[++current_icon] << std::endl;
  }
  
private:
  const char **icons;
  size_t current_icon;
}

I get a compiler error when I try to initialize a class Displayby passing it a constexpr icon :

Display i1(icons1); 
Display i2(icons2);

I tried casting it to const char**, but the compiler complains that the cast removes the property.

Tadman

The declaration here is unnecessarily complicated. Try to keep it simple:

static const char* icons1[] = {
  "icon1_A", "icon1_B"
};

It helps if you pass these as arguments and you need to know exactly how many terminators there are:

static const char* icons1[] = {
  "icon1_A", "icon1_B", nullptr
};

You can iterate through this list until you click nullptrand then stop.

constexprUnsurprisingly, it's used in const expressions , just as more complex expressions contain operators and/or functions to be evaluated. It's not necessary here because nothing exotic happens, it's a plain and simple conststatement.

What's worth noting here is that the memory layout char**and char[3][40]are completely different . In the first case, you have an array of pointers that point to arbitrary memory locations; in the second, you actually have a contiguous block of 120 bytes with no pointers at all. This cannot be recast to other forms, it requires creating a new array of pointers.

That being said, your function assumes that these pointers will have an indeterminate lifetime, which can be risky. It's almost always safer std::stringto use it because they std::vector<std::string>can be passed around.

Related


How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

Pass an array of String as parameter

Surrender to Taclan: String arrays can be declared and initialized in the following ways: String[] str = {"A", "B"}; But for a method that accepts an array of Strings as a parameter, why can't the same method be used in it? For example: if in the code below,

Pass an array of String as parameter

Surrender to Taclan: String arrays can be declared and initialized in the following ways: String[] str = {"A", "B"}; But for a method that accepts an array of Strings as a parameter, why can't the same method be used in it? For example: if in the code below,

Pass an array of String as parameter

Surrender to Taclan: String arrays can be declared and initialized in the following ways: String[] str = {"A", "B"}; But for a method that accepts an array of Strings as a parameter, why can't the same method be used in it? For example: if in the code below,

Pass an array of String as parameter

Surrender to Taclan: String arrays can be declared and initialized in the following ways: String[] str = {"A", "B"}; But for a method that accepts an array of Strings as a parameter, why can't the same method be used in it? For example: if in the code below,

Pass string or array as parameter in bash

Jon Cohen I have excludesa variable which is a list of regular expressions to pass to grep: $ echo $excludes -e re_1 -e re_2 -e re_3... I would like to be able to do something like $ my | pipeline | grep -v "${excludes}" But it doesn't work. I also tried to

Pass string or array as parameter in bash

Jon Cohen I have excludesa variable which is a list of regular expressions to pass to grep: $ echo $excludes -e re_1 -e re_2 -e re_3... I would like to be able to do something like $ my | pipeline | grep -v "${excludes}" But it doesn't work. I also tried to

Pass string or array as parameter in bash

Jon Cohen I have excludesa variable which is a list of regular expressions to pass to grep: $ echo $excludes -e re_1 -e re_2 -e re_3... I would like to be able to do something like $ my | pipeline | grep -v "${excludes}" But it doesn't work. I also tried to

how can i pass a string array parameter to work in delphi

Adam Morochek I have a problem with Delphi. I wrote a function like this: function MyFunction(arr: array of AnsiString): Boolean; begin //code here end; Now when I pass the array AnsiStringdirectly to the function like this , everything works fine: MyFuncti