c++17

How to enable C++17 support in VSCode C++ Extension

巧了我就是萌 提交于 2019-12-10 09:35:41
问题 I keep on getting error squiggles on std::string_view, but I am able to build just fine. Is there a way to tell intellisense or the C++ linter to use C++17? The specific error I get is: namespace "std" has no member "string_view" 回答1: There's a posting in their GitHub issue tracker about this: std::string_view intellisense missing (CMake, VC++ 2017). In another issue, it is said that the extension defaults to C++17, but does not yet support all of C++17 features: Setting C++ standard. This is

if constexpr instead of tag dispatch

≡放荡痞女 提交于 2019-12-10 05:45:14
问题 I want to use if constexpr instead of tag dispatching, but I am not sure how to use it. Example code below. template<typename T> struct MyTag { static const int Supported = 0; }; template<> struct MyTag<std::uint64_t> { static const int Supported = 1; }; template<> struct MyTag<std::uint32_t> { static const int Supported = 1; }; class MyTest { public: template<typename T> void do_something(T value) { // instead of doing this bool supported = MyTag<T>::Supported; // I want to do something like

Template non-type parameter deduction

泪湿孤枕 提交于 2019-12-10 05:16:55
问题 Is it possible to deduce template value (not type) for a c++17 function? The function foo: template<int I> int foo() { return (I); } Can be called via: foo<5>(); And will return 5. Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example: template<int I = x> int bar(const int x) { return (I); } This obviously wont work (because for one x is required before its declaration), but might there be some C+

Template template partial specialization only working with -std=c++1z with g++

不问归期 提交于 2019-12-10 03:45:58
问题 I have found that the following piece of code: #include <iostream> #include <vector> template <typename T> struct X : std::false_type {}; template <template <typename> class Y, typename U> struct X<Y<U>> : std::true_type {}; int main() { if (X<int>()) std::cout << "wrong\n"; if (X<std::vector<double>>()) std::cout << "correct\n"; return 0; } Only prints correct when compiled with g++-7 with -std=c++1z . Other versions of g++ , clang++ or other std flags fail to produce correct. Is this a bug

Do std::tuple and std::pair support aggregate initialization?

最后都变了- 提交于 2019-12-10 03:37:04
问题 Aggregate initialization requires among other things no user-provided constructors . But std::tuple and std::pair pair have a large set of overloaded constructors. From the point of the core language, are these constructors user-provided or even user-declared ? With C++17 it will be possible to write (update/clarification: where nocopy is a class that can not be copied or moved, such as std::mutex ) auto get_ensured_rvo_str(){ return std::pair(std::string(),nocopy()); } edit: no, it's not

Could not convert from brace-enclosed initializer list to std tuple

匆匆过客 提交于 2019-12-10 03:28:58
问题 As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support

Hashing types at compile-time in C++17/C++2a

岁酱吖の 提交于 2019-12-10 03:22:24
问题 Consider the following code: #include <iostream> #include <type_traits> template <class T> constexpr std::size_t type_hash(T) noexcept { // Compute a hash for the type // DO SOMETHING SMART HERE } int main(int argc, char* argv[]) { auto x = []{}; auto y = []{}; auto z = x; std::cout << std::is_same_v<decltype(x), decltype(y)> << std::endl; // 0 std::cout << std::is_same_v<decltype(x), decltype(z)> << std::endl; // 1 constexpr std::size_t xhash = type_hash(x); constexpr std::size_t yhash =

False-branch of if constexpr not discarded in templated lambda

爱⌒轻易说出口 提交于 2019-12-10 02:17:23
问题 I have a problem with "if constexpr" in a templated lambda. For the sake of argument let's ignore how I got there, but I have a struct foo that is defined in some way to result in something as follows: template<bool condition> struct foo { int a; // Only contains b if condition is true int b; } Now I can define a templated function thtemplate template<bool condition> void print_fun(foo & obj) { /* Do something with obj.a */ if constexpr(condition) /* Do something with obj.b */ };

Why does compiler allow out-of-bounds array access even with constexpr index?

五迷三道 提交于 2019-12-10 01:54:41
问题 For example if we have an std::array and we instantiate an element that is out of bound using constexpr the compiler wouldn't report error: constexpr int EvaluateSpecialArrayIndex(int a) { return a * sizeof(int); } array<int, 5> arr; cout << arr[98] << endl; //compiles fine cout << arr[EvaluateSpecialArrayIndex(4)] << endl; //the same as above Can't we restrict this somehow? 回答1: To ensure that constexpr functions are evaluated at compile time, you must force them to be by making their result

Is the move constructor called after invoking a conversion function?

我与影子孤独终老i 提交于 2019-12-10 01:21:34
问题 Consider this example: struct T { }; struct S { operator T(); }; S s; T t = s; [dcl.init] will take us to [over.match.copy] which will find the conversion function operator T() . But are we done at that point, or do we have to invoke T(T&& rhs) , binding rhs to the return of operator T() via [dcl.init.ref]? Are there any differences with regards to the answer to this question between C++11 and C++1z? 回答1: This falls under [dcl.init]/17.6.3, which is pretty clear about what happens after