c++17

Why references can't be used with compile time functions?

左心房为你撑大大i 提交于 2021-02-08 05:30:10
问题 I have two snippets. The first snippet: #include <string> template <typename T> constexpr bool foo(T&&) { return false; } int main() { std::string a; if constexpr (foo(a)) { } } The second snippet: #include <string> template <typename T> constexpr bool foo(T&&) { return false; } int main() { std::string a; std::string& x = a; if constexpr (foo(x)) { } } The first one compiles, but the second one does not compile (error message: error: the value of ‘x’ is not usable in a constant expression .

Numbers of common distinct difference

拟墨画扇 提交于 2021-02-07 20:22:13
问题 Given two array A and B. Task to find the number of common distinct (difference of elements in two arrays). Example : A=[3,6,8] B=[1,6,10] so we get differenceSet for A differenceSetA=[abs(3-6),abs(6-8),abs(8-3)]=[3,5,2] similiarly differenceSetB=[abs(1-6),abs(1-10),abs(6-10)]=[5,9,4] Number of common elements=Intersection :{differenceSetA,differenceSetB}={5} Answer= 1 My approach O(N^2) int commonDifference(vector<int> A,vector<int> B){ int n=A.size(); int m=B.size(); unordered_set<int>

C++17: Wrapping callable using generic variadic lambda

蓝咒 提交于 2021-02-07 14:48:13
问题 I want to wrap a callable of any type (e.g. a lambda) transparently inside another callable to inject additional functionality. The wrapper's type should have the same characteristics as the original callable: Identical parameter types Identical return type Perfect forwarding of passed arguments Same behaviour when used in SFINAE constructs I attempted to use generic variadic lambdas as wrappers: #include <iostream> #include <type_traits> template<class TCallable> auto wrap(TCallable&&

C++17: Wrapping callable using generic variadic lambda

随声附和 提交于 2021-02-07 14:45:43
问题 I want to wrap a callable of any type (e.g. a lambda) transparently inside another callable to inject additional functionality. The wrapper's type should have the same characteristics as the original callable: Identical parameter types Identical return type Perfect forwarding of passed arguments Same behaviour when used in SFINAE constructs I attempted to use generic variadic lambdas as wrappers: #include <iostream> #include <type_traits> template<class TCallable> auto wrap(TCallable&&

Call non constexpr from constexpr template function

与世无争的帅哥 提交于 2021-02-07 13:35:00
问题 I stumbled on constexpr template functions calling non constexpr functions: In the following snippet bar fails to compile as expected due to the call of non constexpr set but foo compiles. Can anyone tell me the reason why foo compiles? template<class T> void set(T& x){ x++; } template<class T> constexpr void foo(T& x){ set<T>(x); } constexpr void bar(int& x){ set<int>(x); } void bar(){ int x = 5; foo(x); bar(x); } The compiler fails to compile with the error: <source>: In function 'constexpr

Call non constexpr from constexpr template function

落花浮王杯 提交于 2021-02-07 13:33:27
问题 I stumbled on constexpr template functions calling non constexpr functions: In the following snippet bar fails to compile as expected due to the call of non constexpr set but foo compiles. Can anyone tell me the reason why foo compiles? template<class T> void set(T& x){ x++; } template<class T> constexpr void foo(T& x){ set<T>(x); } constexpr void bar(int& x){ set<int>(x); } void bar(){ int x = 5; foo(x); bar(x); } The compiler fails to compile with the error: <source>: In function 'constexpr

Will std::experimental::optional<> support references?

三世轮回 提交于 2021-02-07 11:49:18
问题 At the moment, boost::optional<> supports references but the std::experimental::optional<> on my system from libstdc++ does not. Is this reflective of what might make it into the standard? I know that the optional proposal author spun off optional references as a separate proposal so that the main optional proposal would have a better chance of being accepted. Was the proposal for optional references rejected or did work on it stop? 回答1: Is this reflective of what might make it into the

Will std::experimental::optional<> support references?

孤街醉人 提交于 2021-02-07 11:49:04
问题 At the moment, boost::optional<> supports references but the std::experimental::optional<> on my system from libstdc++ does not. Is this reflective of what might make it into the standard? I know that the optional proposal author spun off optional references as a separate proposal so that the main optional proposal would have a better chance of being accepted. Was the proposal for optional references rejected or did work on it stop? 回答1: Is this reflective of what might make it into the

Cannot initialize std::variant with various lambda expressions

大城市里の小女人 提交于 2021-02-07 11:23:07
问题 I'm playing with std::variant, lambdas and std::future , and got super weird results when I tried to compose them together. Here are examples: using variant_t = std::variant< std::function<std::future<void>(int)>, std::function<void(int)> >; auto f1 = [](int) { return std::async([] { return 1; }); }; auto f2 = [](int) { return std::async([] { }); }; variant_t v1(std::move(f1)); // !!! why DOES this one compile when it SHOULDN'T? auto idx1 = v1.index(); //equals 1. WHY? variant_t v2(std::move

C++ recursive_directory_iterator miss some files

做~自己de王妃 提交于 2021-02-07 10:48:15
问题 I'm trying to get all files in directory through c++17 on my visual studio 2017 but I've just encountered a really weird problem. If I specify directory like this I can get all files without any problem: for (auto& p : std::filesystem::recursive_directory_iterator("C:\\Users\\r00t\\AppData\\Roaming\\Mozilla")) { if (std::filesystem::is_regular_file(p.path())) { std::cout << p.path() << std::endl; } } But I need all file list on APPDATA, and I'm trying to get path with getenv() function and