Unable to declare static constexpr char []

I read all the answers related to this problem, but to be honest, I'm not sure if I fully understood the solution. I am using C ++ 11.

Let's say that I really would like to declare something like static constexpr char value[] = "foo".

If I use NetBeans / TDM_MINGW, I get an error message, which I believe is a link reporting an undefined reference to "variable_name" .

Trying the same code in MS VS 2015, I get the expression "not evaluated by constant . "

Simple static constexpr char *solves the problem, but I have lost the ability to use type expressions sizeof.

Simple and simple questions (if possible, simple anders):

  • Is there a way to declare inside / ? static constexpr char []structclass
  • If 1) is false, is there a better solution to overcome this? ???? static constexpr char *
  • Or is the old still the best approach for this case? static const char []
  • I tested a solution that works, but is far from "clean static constexpr array<char,50> getConstExpr(){ return array<char,50> {"Hell"} }. " It works fine, but I have to declare a char size std::array(
+4
source share
3 answers

1) Is there a way to declare static constexpr char []inside struct/ class?

Yes; it's simple.

Below is a complete working example

struct bar
 { static constexpr char value[] = "foo"; };

constexpr char bar::value[];

int main ()
 {
   std::cout << bar::value << std::endl; // print foo
 }

I assume you forgot the line bar::value[].

2) 1) , static constexpr char *????

.

3) static const char [] - ?

, ; C- ++ 11 std::array

4) , , , "" [...] , char std::array: (

( , ++ 14, ++ 11), "Hell", .

std::make_index_sequence std::index_sequence, char[] std::array.

template <std::size_t Dim, std::size_t ... Is>
constexpr std::array<char, Dim> gceH (char const (&str)[Dim],
                                      std::index_sequence<Is...> const &)
 { return { { str[Is]... } }; }

template <std::size_t Dim>
constexpr std::array<char, Dim> getConstExpr (char const (&str)[Dim])
 { return gceH(str, std::make_index_sequence<Dim>{}); }

int main ()
 {
   constexpr auto f = getConstExpr("Hell");

   static_assert( 5U == f.size(), "!" );
 }

- EDIT -

Swift (!), , char, getConstExpr() .

So getConstExpr(), (gceH()) :

template <typename T, std::size_t Dim, std::size_t ... Is>
constexpr std::array<T, Dim> gceH (T const (&str)[Dim],
                                   std::index_sequence<Is...> const &)
 { return { { str[Is]... } }; }

template <typename T, std::size_t Dim>
constexpr std::array<T, Dim> getConstExpr (T const (&str)[Dim])
 { return gceH(str, std::make_index_sequence<Dim>{}); }
+3

C, ++ 17, std:: string_view..., .;)

perm-link .

#include <iostream>
#include <string_view>

int main ()
{
    constexpr std::string_view foo ( "Hell" );
    static_assert ( 4U == foo.size (), "!" );

    std::cout << "Done";
    return EXIT_SUCCESS;
}
0

Since C ++ 17 you do not need anything special, because member variables are static constexprimplicit inline.

The following will work:

#include <iostream>

struct S
{
    static constexpr char value[] = "Meow!\n";
};

int main()
{
    std::cout << S::value;
}
0
source

Source: https://habr.com/ru/post/1683002/


All Articles