How do I get the return type of a member function without an object?
You don’t need that- remember that since decltype doesn’t evaluate its argument, you can just call on nullptr. decltype(((T*)nullptr)->foo()) footype;
You don’t need that- remember that since decltype doesn’t evaluate its argument, you can just call on nullptr. decltype(((T*)nullptr)->foo()) footype;
std::chrono::duration has two template parameters, the second being exactly the unit of measure. You can invoke std::chrono::duration_cast to cast from one duration type to another. Also, there is a predefined duration type for milliseconds: std::chrono::milliseconds. Composing this together: auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(foo – now); To get the actual number of milliseconds, use duration::count: auto ms … Read more
When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary proposal, to increase the chances of getting optional values approved and put into what became C++14. While optional didn’t quite make it into C++14 for … Read more
When using reset() the parameter passed to reset need not be a managed object (nor can it be); whereas with = the right hand side must be a managed object. So these two lines give you the same end result: p = std::make_shared<int>(5); // assign to a newly created shared pointer p.reset(new int(5)); // take … Read more
What are the rules here? As there is only one parameter, the rule is that one of the three viable parameter initializations of that parameter must be a better match than both the other two. When two initializations are compared, either one is better than the other, or neither is better (they are indistinguishable). Without … Read more
Aren’t they basically the same? No. Absolutely not. Not even close. Apart from the fact your macro is an int and your constexpr unsigned is an unsigned, there are important differences and macros only have one advantage. Scope A macro is defined by the preprocessor and is simply substituted into the code every time it … Read more
Up through C++03, your first example was valid, but used a deprecated implicit conversion–a string literal should be treated as being of type char const *, since you can’t modify its contents (without causing undefined behavior). As of C++11, the implicit conversion that had been deprecated was officially removed, so code that depends on it … Read more
First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); The first example returns a temporary which is caught by rval_ref. That temporary will have its life extended beyond the rval_ref definition and you can use it as if you had caught it by value. This is very similar to … Read more
It seems to be a matter of ABI. For instance, the Itanium C++ ABI reads: If the parameter type is non-trivial for the purposes of calls, the caller must allocate space for a temporary and pass that temporary by reference. And, further: A type is considered non-trivial for the purposes of calls if it has … Read more
Is there a way to cause a compile-time error with a constexpr function, but not do anything at run time? You can use the exact same trick, but instead of using a throw-expression, use an expression that is not a constant expression but does what you want at runtime. For instance: int runtime_fallback(int x) { … Read more