c++17

fold expression and function name lookup

落爺英雄遲暮 提交于 2021-02-20 19:58:35
问题 I am learning fold expressions in C++17. I have the following code #include <iostream> #include <vector> namespace io { template<typename T> std::istream &operator>>(std::istream &in, std::vector<T> &vec) { for (auto &x : vec) in >> x; return in; } template<class... Args> void scan(Args &... args) { (std::cin >> ... >> args); } }// namespace io int main() { std::vector<int> s(1), t(1); io::scan(s, t); std::cout << s[0] << ' ' << t[0] << '\n'; } Using GCC 9.3.0, the code compiles and runs

How to initialize a constexpr std::array with templated constexpr member functions?

走远了吗. 提交于 2021-02-19 08:45:38
问题 This is a follow up of my question given here. In the end I want to create a constexpr std::array containing text with an appended running index. I wanted to try a different approach than in the previous question. Nearly everything, what I do in the below code is constexpr. But maybe, it is simply the old problem of returning a pointer to a no longer existing variable. But, I doubt this. Please see the following code, where the not working line in function main is marked. #include <iostream>

How to read a UTF-16 text file in C++17

不打扰是莪最后的温柔 提交于 2021-02-19 05:57:06
问题 I am very new to C++. I want to read a UTF-16 text file in C++17 in Visual Studio 2019. I have tried several methods in the internet (including StackOverflow) but none of them worked, and some of them didn't compile (I think they only support older compilers). I am trying to achieve this without using any 3rd party libraries. This reads a text file, but it has some weird characters and spaces between each letter. // open file for reading std::wifstream istrm(filename, std::ios::binary); if (

What is the reason why clang and gcc do not implement std::hardware_{constructive,destructive}_interference_size?

旧时模样 提交于 2021-02-19 05:42:13
问题 I know the answer could be that they did not prioritize it, but it really feels like intentional omission, they already have plenty of C++20 core language/library features and this C++17 feature is still not implemented. In fact according to this table it is the only C++17 library feature that both clang and gcc did not implement. 来源: https://stackoverflow.com/questions/62025586/what-is-the-reason-why-clang-and-gcc-do-not-implement-stdhardware-constructiv

What is the reason why clang and gcc do not implement std::hardware_{constructive,destructive}_interference_size?

梦想的初衷 提交于 2021-02-19 05:42:06
问题 I know the answer could be that they did not prioritize it, but it really feels like intentional omission, they already have plenty of C++20 core language/library features and this C++17 feature is still not implemented. In fact according to this table it is the only C++17 library feature that both clang and gcc did not implement. 来源: https://stackoverflow.com/questions/62025586/what-is-the-reason-why-clang-and-gcc-do-not-implement-stdhardware-constructiv

How to create a tuple of vectors of type deduced from a variadic template in C++17?

▼魔方 西西 提交于 2021-02-19 05:04:11
问题 I have implemented a collection class that converts a vector of tuples to a tuple of vectors (it is essentially an AOS to SOA conversion). This code works for this example of two template classes. I was trying to make it more generic by using variadic templates. In order to do that I need to create the type for the member variable m_col . In C++17, is it possible to convert a tuple to a tuple of vectors? So the type of the member variance m_col in this example will be generated automatically

waiting on worker thread using std::atomic flag and std::condition_variable

六月ゝ 毕业季﹏ 提交于 2021-02-19 02:46:07
问题 Here is a C++17 snippet where on thread waits for another to reach certain stage: std::condition_variable cv; std::atomic<bool> ready_flag{false}; std::mutex m; // thread 1 ... // start a thread, then wait for it to reach certain stage auto lock = std::unique_lock(m); cv.wait(lock, [&]{ return ready_flag.load(std::memory_order_acquire); }); // thread 2 ... // modify state, etc ready_flag.store(true, std::memory_order_release); std::lock_guard{m}; // NOTE: this is lock immediately followed by

Preventing implicit conversion operator only for binary operators

走远了吗. 提交于 2021-02-19 02:22:09
问题 I'm having an issue that I've boiled down to the following, where an == operator usage compiles even though it's supposed to fail (C++17, tested on GCC 5.x, 8.x, and 9.x): template <int N> struct thing { operator const char * () const { return nullptr; } bool operator == (const thing<N> &) const { return false; } }; int main () { thing<0> a; thing<1> b; a == b; // i don't want this to compile, but it does } The reason it is compiling is because the compiler is choosing to do this: (const char

if constexpr vs sfinae

。_饼干妹妹 提交于 2021-02-19 00:55:05
问题 With the introduction of if constexpr in c++17 , some problems which were solved by using compile-time SFINAE in c++14 / c++11 can now be solved using if constexpr , with an easier syntax. Consider, e.g., the following basic example of a compile-time recursion to produce a subroutine which prints a variable number of arguments. #include <iostream> #include <type_traits> template <typename T> void print_sfinae(T&& x) { std::cout << x << std::endl; } template <typename T0, typename... T> std:

Is “if constexpr” useful outside of templates?

时光怂恿深爱的人放手 提交于 2021-02-18 22:51:52
问题 I'm trying to understand if constexpr fully. I understand, that if if constexpr(expr) used in a template, and expr is dependent on a template parameter, then during instantiation, only one of the then / else branches will be instantiated, the other will be discarded. I've got two questions: Is it true, that if expr is not dependent on a template parameter, then no branches of if constexpr(expr) will be discarded? If yes, where does the standard say so? I don't see where the standard has the