Are the experimental features of modern C++ reliable for long-term projects?

Is it guaranteed that all compliant compilers have the same experimental features? No, experimental features are optional. Are experimental features prone to big changes that make them unreliable? Yes, the C++ committee might even decide to abandon a feature or in the process of standardization a defect might come up that would force a feature … Read more

Why is GCC warning me about a fallthrough even when I use [[fallthrough]]?

You are missing a semicolon after the attribute: case 0: std::cout << “a\n”; [[fallthrough]]; // ^ case 1: The [[fallthrough]] attribute is to be applied to an empty statement (see P0188R1). The current Clang trunk gives a helpful error in this case: error: fallthrough attribute is only allowed on empty statements [[fallthrough]] ^ note: did … Read more

Understanding std::hardware_destructive_interference_size and std::hardware_constructive_interference_size

The intent of these constants is indeed to get the cache-line size. The best place to read about the rationale for them is in the proposal itself: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html I’ll quote a snippet of the rationale here for ease-of-reading: […] the granularity of memory that does not interfere (to the first-order) [is] commonly referred to as … Read more

How to get the file size in bytes with C++17

<filesystem> (added in C++17) makes this very straightforward. #include <cstdint> #include <filesystem> // … std::uintmax_t size = std::filesystem::file_size(“c:\\foo\\bar.txt”); As noted in comments, if you’re planning to use this function to decide how many bytes to read from the file, keep in mind that… …unless the file is exclusively opened by you, its size can be … Read more

How to efficiently get a `string_view` for a substring of `std::string`

There’s the free-function route, but unless you also provide overloads for std::string it’s a snake-pit. #include <string> #include <string_view> std::string_view sub_string( std::string_view s, std::size_t p, std::size_t n = std::string_view::npos) { return s.substr(p, n); } int main() { using namespace std::literals; auto source = “foobar”s; // this is fine and elegant… auto bar = sub_string(source, 3); … Read more

Why is there no implicit conversion from std::string_view to std::string?

The problem is that std::string_view -> std::string makes a copy of the underlying memory, complete with heap allocation, whereas the implicit std::string -> std::string_view does not. If you’ve bothered to use a std::string_view in the first place then you obviously care about copies, so you don’t want one to happen implicitly. Consider this example: void … Read more

What are template deduction guides and when should we use them?

Template deduction guides are patterns associated with a template class that tell the compiler how to translate a set of constructor arguments (and their types) into template parameters for the class. The simplest example is that of std::vector and its constructor that takes an iterator pair. template<typename Iterator> void func(Iterator first, Iterator last) { vector … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)