c++17

DLL exporting causing issues with unique pointers

走远了吗. 提交于 2021-02-05 06:25:05
问题 I've got two files: Header.h #pragma once #ifdef UNIQUEPTRISSUE_EXPORTS #define UNIQUEPTRISSUE_API __declspec(dllexport) #else #define UNIQUEPTRISSUE_API __declspec(dllimport) #endif UniquePtrIssue.cpp #include "stdafx.h" #include "Header.h" #include <memory> #include <vector> class UNIQUEPTRISSUE_API ClassA { }; class UNIQUEPTRISSUE_API ClassB { private: std::vector<std::unique_ptr<ClassA>> x; }; Compiling raises the following error: 1>d:\program files (x86)\microsoft visual studio\2017

deduction guides for std::array

ぃ、小莉子 提交于 2021-02-05 06:16:45
问题 I go through the book C++ template unique quide and I try to understand how the deduction guides for std::array works. Regarding the definition of the standard the following is the declaration template <class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>; For example if in main a array created as std::array a{42,45,77} How the deduction takes place? Thank you 回答1: How the deduction takes place? It's simple. Calling std::array a{42,45,77} match array(T, U...) with T = decltype(42

“Nested” class-template argument deduction with parentheses: GCC vs. clang

佐手、 提交于 2021-02-05 04:59:20
问题 Related, but (IMHO) different: Nested template argument deduction for class templates not working The following C++17 code is rejected from GCC 8, but clang compiles it without any issues. The GCC's error message is included as a comment just before the problematic line. Which compiler is correct here? https://godbolt.org/z/WG6f7G template<class T> struct Foo { Foo(T) {} }; template<class T> struct Bar { Bar(T) {}; }; void works() { Bar bar{1};// {} Foo foo(bar);// () } void works_too() { Foo

Compiler error with C++17 static inline members

人盡茶涼 提交于 2021-02-04 19:20:10
问题 I'm using Microsoft Visual Studio 2017, and from what I've seen it does support C++17 static inline class variables. My issue is that if I leave all members unitialised it works fine but I get a compiler error when initialising certain members. In the following example: #include <iostream> class Foo { public: static inline int a; static inline int b; static inline int c; }; int main() { Foo foo; std::cout << foo.a; // Prints 0 std::cin.ignore(); return 0; } It works fine. In the following

How to emulate deduction guides for template aliases?

旧时模样 提交于 2021-02-04 17:53:06
问题 Consider the following: template <typename T, std::size_t N> struct my_array { T values[N]; }; We can provide deduction guides for my_array , something like template <typename ... Ts> my_array (Ts ...) -> my_array<std::common_type_t<Ts...>, sizeof...(Ts)>; Now, suppose that my_array<T, 2> has some very special meaning (but only meaning, the interface & implementation stay the same), so that we'd like to give it a more suitable name: template <typename T> using special = my_array<T, 2>; It

Unpacking variadic tuples in c++17

戏子无情 提交于 2021-02-04 17:27:50
问题 Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence? Anything better than this: template <typename ...I> class MultiIterator { public: MultiIterator(I const& ...i) : i(i...) {} MultiIterator& operator ++() { increment(std::index_sequence_for<I...>{}); return *this; } private: template <std::size_t ...C> void increment(std::index_sequence<C...>) { std::ignore = std::make_tuple(++std::get<C>(i)...); } std::tuple<I...> i; }

Template parameters of function type with auto return type arguments of previous template parameter types

一笑奈何 提交于 2021-02-04 15:39:19
问题 I have a template with two parameters: the first is a type, and the second is a function pointer with an argument whose type is the first template parameter. This MCVE works: void returnsVoid(int x) { } template <typename T, void (*Func)(T)> struct foo { void bar(T t) { Func(t); } }; int main(int, char *[]) { foo<int, returnsVoid> a; // ok } However, when I change the return type of the second template parameter to auto (as explained in this related question), I get an error: void returnsVoid

Template parameters of function type with auto return type arguments of previous template parameter types

心不动则不痛 提交于 2021-02-04 15:39:19
问题 I have a template with two parameters: the first is a type, and the second is a function pointer with an argument whose type is the first template parameter. This MCVE works: void returnsVoid(int x) { } template <typename T, void (*Func)(T)> struct foo { void bar(T t) { Func(t); } }; int main(int, char *[]) { foo<int, returnsVoid> a; // ok } However, when I change the return type of the second template parameter to auto (as explained in this related question), I get an error: void returnsVoid

How to create a `range`-like iterable object of floats?

一曲冷凌霜 提交于 2021-02-04 14:26:09
问题 I want to create a range -like construct in c++, that will be used like this: for (auto i: range(5,9)) cout << i << ' '; // prints 5 6 7 8 for (auto i: range(5.1,9.2)) cout << i << ' '; // prints 5.1 6.1 7.1 8.1 9.1 Handling the integer case is relatively easy: template<typename T> struct range { T from, to; range(T from, T to) : from(from), to(to) {} struct iterator { T current; T operator*() { return current; } iterator& operator++() { ++current; return *this; } bool operator==(const

How to create a `range`-like iterable object of floats?

一世执手 提交于 2021-02-04 14:25:24
问题 I want to create a range -like construct in c++, that will be used like this: for (auto i: range(5,9)) cout << i << ' '; // prints 5 6 7 8 for (auto i: range(5.1,9.2)) cout << i << ' '; // prints 5.1 6.1 7.1 8.1 9.1 Handling the integer case is relatively easy: template<typename T> struct range { T from, to; range(T from, T to) : from(from), to(to) {} struct iterator { T current; T operator*() { return current; } iterator& operator++() { ++current; return *this; } bool operator==(const