c++17

select a set of values from tuple with run-time index

心不动则不痛 提交于 2020-01-05 02:28:50
问题 Short introduction to my questions: i'm trying to implement a "sort of" relational database using stl containers. This is just for fun/educational purpose, so no need for answers like "use this library", "this is absolutely useless" and so on. I know title is a little bit confusing at this point, but we will reach the point (suggestions for improvement to title are really welcome). I proceeded with little steps: i can build table as vector of maps from columns name to their values => std:

SFINAE/enable_if based on the contents of a string parameter?

∥☆過路亽.° 提交于 2020-01-04 05:57:27
问题 I can not get my head around the following problem. I don't even really know how I could approach it. Consider this code: struct fragment_shader { std::string mPath; }; struct vertex_shader { std::string mPath; }; template <typename T> T shader(std::string path) { return T{ path }; } To create the different structs, I can write the following: auto fragmentShader = shader<vertex_shader>("some_shader.frag"); auto vertexShader = shader<fragment_shader>("some_shader.vert"); I am wondering, if it

A function template that accepts both std::vector and QVector?

纵饮孤独 提交于 2020-01-04 05:24:11
问题 Suppose I have a function called loadData() which takes a container (to be filled with data) and a CSV file. I need the following overloads: loadData(std::vector<double>& data, const std::string& file); loadData(QVector<double>& data, const std::string& file); loadData(std::vector<std::complex<double>>& data, const std::string& file); loadData(QVector<std::complex<double>>& data, const std::string& file); loadData(std::vector<std::vector<double>>& data, const std::string& file); loadData

Object access using reinterpret_cast for “struct {double, int}”-like object

泄露秘密 提交于 2020-01-04 05:21:09
问题 Accessing objects via reinterpret_cast ed pointers and related UB has been extensively discussed here. After reading questions and answers, I'm still not sure about proper using uninitialized memory with POD types. Suppose I want to "emulate" struct { double d; int i; }; by manually allocating memory for data members and suppose (for simplicity) that no padding is needed before i . Now, I do this: // (V1) auto buff = reinterpret_cast<char*>(std::malloc(sizeof(double) + sizeof(int))); auto d

constexpr lambda / ‘x’ does not name a type; did you mean ‘x’?

三世轮回 提交于 2020-01-03 17:10:48
问题 I am experimenting with C++17's constexpr lambdas to get compile time strings: #include <utility> template <char...> struct str { constexpr auto operator==(const str&) const { return true; } void foo() const; }; template <typename S, std::size_t... Ns> constexpr auto make_str(S s, std::index_sequence<Ns...>) { return str<s()[Ns]...>{}; } #define LIT(s) \ make_str([]() { return s; }, std::make_index_sequence<sizeof(s) - 1>{}) constexpr auto x = LIT("hansi"); constexpr auto y = x; static_assert

Differences between c++14 std::experimental::filesystem::v1 and c++17 std::filesystem?

守給你的承諾、 提交于 2020-01-03 15:35:23
问题 I found this page, describing the changes between c++14 and c++17: https://isocpp.org/files/papers/p0636r0.html ... It links to this page, which describes the proposed filesystem changes: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0218r0.html I skimmed through it. There are small wording changes to the standard, but the only code change I saw were namespace changes that removed the "experimental" and "v1" parts, so "std::experimental::filesystem::v1" became "std::filesystem",

Why does this get_index implementation fail on VS2017?

梦想的初衷 提交于 2020-01-03 11:49:11
问题 Barry gave us this gorgeous get_index for variants: template <typename> struct tag { }; template <typename T, typename V> struct get_index; template <typename T, typename... Ts> struct get_index<T, std::variant<Ts...>> : std::integral_constant<size_t, std::variant<tag<Ts>...>(tag<T>()).index()> { }; To be used as follows: using V = variant<A, B, C>; constexpr const size_t N = get_index<B, V>::value; // 1 It works great in Clang (OSX). But in Visual Studio 2017 I'm getting the following:

In C++, how to make a variant that can contain a vector of of same variant?

守給你的承諾、 提交于 2020-01-03 10:54:23
问题 I a trying to make a std::variant that can contain a vector of the same variant: class ScriptParameter; using ScriptParameter = std::variant<bool, int, double, std::string, std::vector<ScriptParameter> >; I am getting ScriptParameter redefinition. It think it is possibly because a template parameter cannot be forward declared? Is there a way to achieve a variant that could also contain an array of same typed variants? 回答1: Since the forward declaration says ScriptParameter is a class, you can

C++ conversion operator to chrono::duration - works with c++17 but not C++14 or less

微笑、不失礼 提交于 2020-01-03 08:35:30
问题 The following code compiles with gcc 7.1.0 with C++17 set but does not compile with C++14 set (or Visual Studio 2017). It is easy to reproduce on Wandbox. What has to be done to make it work with C++11/14? #include <iostream> #include <chrono> int main() { struct Convert { operator std::chrono::milliseconds() { std::cout << "operator std::chrono::milliseconds" << std::endl; return std::chrono::milliseconds(10); } operator int64_t () { std::cout << "operator int64_t" << std::endl; return 5; }

Can an inline variable be changed after initialization in C++17?

心已入冬 提交于 2020-01-03 07:19:32
问题 My scenario is the following (it worked in clang but not in gcc) liba.hpp: inline int MY_GLOBAL = 0; libother.cpp: (dll) #include "myliba.hpp" void myFunc() { // MYGLOBAL = 28; } someexe.cpp: RunAppThatUsesBothLibAandLibOther(); The problem is that the inline variable was showing 0 in places where I expected 28 because it was alrady modified at run-time. MSVC disagrees with this, but clang does the thing I would expect. The question is: can inline variables be modified at run-time in my