Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

deleted 83 characters in body; edited tags
Source Link
Barry
  • 312.2k
  • 33
  • 739
  • 1.1k
#include <vector>
#include <string>
#include <algorithm>
constexpr std::vector<std::string> 
split(std::string_view strv, std::string_view delims = " ") {
 std::vector<std::string> output;
 size_t first = 0;
 while (first < strv.size()) {
 const auto second = strv.find_first_of(delims, first);
 if (first != second)
 output.emplace_back(strv.substr(first, second-first));
 if (second == std::string_view::npos)
 break;
 first = second + 1;
 }
 return output;
}
constexpr size_t numWords(std::string_view str) {
 const auto words = split(str);
 return words.size();
}
int main() {
 static_assert(numWords("hello world abc xyz") == 4);
}
#include <vector>
#include <string>
#include <algorithm>
constexpr std::vector<std::string> 
split(std::string_view strv, std::string_view delims = " ") {
 std::vector<std::string> output;
 size_t first = 0;
 while (first < strv.size()) {
 const auto second = strv.find_first_of(delims, first);
 if (first != second)
 output.emplace_back(strv.substr(first, second-first));
 if (second == std::string_view::npos)
 break;
 first = second + 1;
 }
 return output;
}
constexpr size_t numWords(std::string_view str) {
 const auto words = split(str);
 return words.size();
}
int main() {
 static_assert(numWords("hello world abc xyz") == 4);
}

const auto words = split(str)

const auto words = split(str);

constexpr auto words = split(str)

constexpr auto words = split(str);
#include <vector>
#include <string>
#include <algorithm>
constexpr std::vector<std::string> 
split(std::string_view strv, std::string_view delims = " ") {
 std::vector<std::string> output;
 size_t first = 0;
 while (first < strv.size()) {
 const auto second = strv.find_first_of(delims, first);
 if (first != second)
 output.emplace_back(strv.substr(first, second-first));
 if (second == std::string_view::npos)
 break;
 first = second + 1;
 }
 return output;
}
constexpr size_t numWords(std::string_view str) {
 const auto words = split(str);
 return words.size();
}
int main() {
 static_assert(numWords("hello world abc xyz") == 4);
}

const auto words = split(str)

constexpr auto words = split(str)

#include <vector>
#include <string>
#include <algorithm>
constexpr std::vector<std::string> 
split(std::string_view strv, std::string_view delims = " ") {
 std::vector<std::string> output;
 size_t first = 0;
 while (first < strv.size()) {
 const auto second = strv.find_first_of(delims, first);
 if (first != second)
 output.emplace_back(strv.substr(first, second-first));
 if (second == std::string_view::npos)
 break;
 first = second + 1;
 }
 return output;
}
constexpr size_t numWords(std::string_view str) {
 const auto words = split(str);
 return words.size();
}
int main() {
 static_assert(numWords("hello world abc xyz") == 4);
}
const auto words = split(str);
constexpr auto words = split(str);
Source Link
getsoubl
  • 1.1k
  • 14
  • 32

C++20 Constexpr vector

The latest C++20 standard supports dynamic allocation memory in compile time with a limitation. The memory allocation cannot "go outside" the constant expression. That means that vector cannot be return and stored in a constexpr variable.

In the cppstories article there is the following example

#include <vector>
#include <string>
#include <algorithm>
constexpr std::vector<std::string> 
split(std::string_view strv, std::string_view delims = " ") {
 std::vector<std::string> output;
 size_t first = 0;
 while (first < strv.size()) {
 const auto second = strv.find_first_of(delims, first);
 if (first != second)
 output.emplace_back(strv.substr(first, second-first));
 if (second == std::string_view::npos)
 break;
 first = second + 1;
 }
 return output;
}
constexpr size_t numWords(std::string_view str) {
 const auto words = split(str);
 return words.size();
}
int main() {
 static_assert(numWords("hello world abc xyz") == 4);
}

This example compiles successfully. If I change

const auto words = split(str)

to

constexpr auto words = split(str)

I get following error

example.cpp (27): error C2131: expression did not evaluate to a constant (27): note: failure was caused by a read of a variable outside its lifetime (27): note: see usage of 'str' Compiler returned: 2

Based on the standard this expected since the compilers don’t support so-called "non-transient" memory allocations. But why no error is reported when const is used. In bot cases the code is executed in compile time

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /