c++17

Is it possible to detect the default parameters of a function at compile-time?

我与影子孤独终老i 提交于 2019-12-10 21:44:11
问题 #include <thread> #include <functional> using namespace std; void f(int n = 7) {} void g(function<void()> fn) { fn(); // same as f(7) } template<typename Callable> auto GetDefaultArg(Callable fn, size_t arg_ordinal) { // What to put here? } int main() { auto fn = bind(f, GetDefaultArg(f, 1)); g(fn); } As illustrated in the code above, I want to implement a template function GetDefaultArg to detect the default paratemters of a function. It it possible in current C++? 回答1: No you can't detect

std::string_view and std::string in std::unordered_set [duplicate]

佐手、 提交于 2019-12-10 21:43:10
问题 This question already has an answer here : C++ unordered_map<string, …> lookup without constructing string (1 answer) Closed last year . Let's say you have an std::unordered_set<std::string> . You have an std::string_view object that you want to search for in the container. Problem is, you don't want to create a std::string from your std::string_view , as this kind of defeats the purpose of using std::string_view in the first place. However, it seems that std::string_view should be usable as

An error is issued by gcc relative to parsing type-id in a new expression

僤鯓⒐⒋嵵緔 提交于 2019-12-10 20:13:32
问题 This program #include <cstddef> int main() { const std::size_t N1 = 2; const std::size_t N2 = 3; int ( **p )[N1] = new ( int ( *[N2] )[N1] ); } does not compile using the compiler C++ gcc HEAD 10.0.0 20190. The compiler issues error prog.cc: In lambda function: prog.cc:8:40: error: expected '{' before ')' token 8 | int ( **p )[N1] = new ( int ( *[N2] )[N1] ); | ^ prog.cc: In function 'int main()': prog.cc:8:34: error: no match for 'operator*' (operand type is 'main()::<lambda()>') 8 | int ( *

Template argument deduction for references as arguments

旧街凉风 提交于 2019-12-10 19:59:56
问题 I am trying to profoundly understand Template Argument Deduction. One point I am not understanding is, how I should apply the rules in the standard here for the types A and P for the following case (there is sadly no example on cppreference.com, see below the relevant section) template<typename T> void foo(T t); void call_with_reference(int& r) { foo(r) } P is no reference typ: which gives P := T A := int& -> Match P and A which gives: T is deduced to int& which is cleary wrong. Where is the

How to write function with parameter which type is deduced with 'auto' word?

微笑、不失礼 提交于 2019-12-10 19:44:48
问题 I am searching a clean c++11 (up to c++17) way to write a function that simply writes fps to the output stream with given 'start' and 'stop' times (e.g. given an interval times). So I have this code, for example: #include <iostream> int main(int argc, char** argv) { typedef std::chrono::high_resolution_clock time_t; while (1) { auto start = time_t::now(); // here is the call of function that do something // in this example will be printing std::cout << "Hello world!" auto stop = time_t::now()

Using a non static value as default argument in a function

混江龙づ霸主 提交于 2019-12-10 19:25:40
问题 Is there a nice way to have a non static value as default argument in a function? I've seen some older responses to the same question which always end up in explicitly writing out the overload. Is this still necessary in C++17? What I'd like to do is do something akin to class C { const int N; //Initialized in constructor void foo(int x = this->N){ //do something } } instead of having to write class C { const int N; //Initialized in constructor void foo(){ foo(N); } void foo(int x){ //do

Alternative for quad-nested unordered_map monstrosity?

回眸只為那壹抹淺笑 提交于 2019-12-10 19:03:44
问题 I've been trying out to figure an effective way to store and retrieve a number of objects. Let me explain what I'm trying to achieve, then list the options I've come up with ( But am unhappy with ). The following technically does what I need it to do, but is an obvious no-no: std::unordered_map<uint32_t, std::unordered_map<uint32_t, std::unordered_map<uint32_t, std::unordered_map<uint32_t, Component*>>>> //Scene -> Layer -> Type -> Id -> Component* The most inner map holds the Components

Implement is_destructible with Detected Idiom

馋奶兔 提交于 2019-12-10 18:14:13
问题 Here is my implementation of is_destructible_v: template<class T> struct is_unknown_bound_array : std::false_type {}; template<class T> struct is_unknown_bound_array<T[]> : std::true_type {}; template<typename T, typename U = std::remove_all_extents_t<T>> using has_dtor = decltype(std::declval<U&>().~U()); template<typename T> constexpr bool is_destructible_v = (std::experimental::is_detected_v<has_dtor, T> or std::is_reference_v<T>) and not is_unknown_bound_array<T>::value and not std::is

How to “iterate” over a list of templates at compile time?

我们两清 提交于 2019-12-10 17:55:50
问题 This is the extraction of a follow-up question to this answer. Given the following "loop" technique #pragma once // loop.hpp #include <type_traits> #include <utility> template<std::size_t... indices, class LoopBody> void loop_impl(std::index_sequence<indices...>, LoopBody&& loop_body) { (// C++17's fold expression loop_body(std::integral_constant<std::size_t, indices>{}), ... ); } template<std::size_t N, class LoopBody> void loop(std::integral_constant<std::size_t, N>, LoopBody&& loop_body) {

Are There Restrictions on What can be Passed to auto Template Parameters? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-10 17:44:24
问题 This question already has answers here : Non-type template parameters (4 answers) Closed 12 months ago . In c++17 we got auto template parameters. I was trying to use one to pass an object in this question: Can I Write Relational Operators in Terms of Arithmetic Operations? But directed by AndyG's comment I found that didn't compile :( Given the template function: template <auto T> void foo() There seem to be restrictions on what I can pass as a template parameter. For example, as seen in my