c++14

constexpr depth limit with clang (fconstexpr-depth doesnt seem to work)

和自甴很熟 提交于 2019-12-21 04:21:16
问题 Is there anyway to configure constexpr instantiation depth? I am running with -fconstexpr-depth=4096 (using clang/XCode). But still fail to compile this code with error: Constexpr variable fib_1 must be initialized by a constant expression. The code fails irrespective of whether option -fconstexpr-depth=4096 is set or not. Is this a bug with clang or is expected to behave this way. Note: this works good till fib_cxpr(26), 27 is when it starts to fail. Code: constexpr int fib_cxpr(int idx) {

constexpr depth limit with clang (fconstexpr-depth doesnt seem to work)

大憨熊 提交于 2019-12-21 04:21:02
问题 Is there anyway to configure constexpr instantiation depth? I am running with -fconstexpr-depth=4096 (using clang/XCode). But still fail to compile this code with error: Constexpr variable fib_1 must be initialized by a constant expression. The code fails irrespective of whether option -fconstexpr-depth=4096 is set or not. Is this a bug with clang or is expected to behave this way. Note: this works good till fib_cxpr(26), 27 is when it starts to fail. Code: constexpr int fib_cxpr(int idx) {

Does standard C++11 guarantee that std::async(std::launch::async, func) launches func in separate thread?

匆匆过客 提交于 2019-12-21 04:12:28
问题 Does standard C++11 guarantee that std::async(std::launch::async, func) launches function in separate thread? Working Draft, Standard for Programming Language C++ 2016-07-12: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf 1. On the one hand , C++11-Standard says that if the thread can not be created, then there is an error. This ensures the creation of a new thread (in the absence of errors). § 30.6.8 6 Throws: system_error if policy == launch::async and the implementation

Pointer to member: works in GCC but not in VS2015

一世执手 提交于 2019-12-21 03:52:06
问题 I'm trying to implement a "property" system to convert C++ instances into JSON and vice versa. I took a part of the code from Guillaume Racicot's answer in this question (C++ JSON Serialization) and simplified it. Here is how I proceed. I have a Property class: template <typename Class, typename T> struct Property { constexpr Property(T Class::* member, const char* name) : m_member(member), m_name(name) {} T Class::* m_member; const char* m_name; }; m_member points to a specific member of

Avoiding extra move in make_unique/make_shared/emplace/etc for structures that use aggregate initialization

情到浓时终转凉″ 提交于 2019-12-21 03:49:31
问题 std::make_unique() (and similar functions) have a little problem: #include <cstdio> #include <memory> using namespace std; struct S { S() { printf("ctor\n"); } ~S() { printf("dtor\n"); } S(S const&) { printf("cctor\n"); } S(S&&) { printf("mctor\n"); } }; S foo() { return S(); } int main() { { printf("--------------- case 1 ---------------\n"); unique_ptr<S> s1 = make_unique<S>( foo() ); } { printf("--------------- case 2 ---------------\n"); unique_ptr<S> s2 { new S( foo() ) }; } } Output: --

Is it well-defined to cast xvalues to lvalues for passing to functions?

落爺英雄遲暮 提交于 2019-12-21 03:32:54
问题 Recently I've discovered that sometimes being able to turn rvalues temporarily into lvalues can be useful for me. I've been using the following tool: #include <type_traits> template <typename T> inline constexpr std::remove_reference_t<T> &lvalue(T &&r) noexcept { return static_cast<std::remove_reference_t<T> &>(r); } It's useful when you have to use functions that require lvalues as arguments, but you don't have any interest in what those particular values get changed into. For when you are

CRTP and c++1y return type deduction

女生的网名这么多〃 提交于 2019-12-21 03:31:07
问题 I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template<typename Derived> struct Base { auto foo() { return static_cast<Derived*>(this)->foo_impl(); } }; struct Derived: public Base<Derived> { auto foo_impl() -> int { return 0; } }; int main() { Derived b; int i = b.foo(); (void)i; } I assumed that the return type from Base<Derived>::foo was a decltype of the expression returned,

Why isn't the “noexcept” specifier part of the function type?

南笙酒味 提交于 2019-12-21 03:18:31
问题 I don't get it why? I don't think compatibility should be a problem as functions declared without the specifier actually have it implicitly defined to false. If it's about name mangling - can we just suppose that old one (existing) will imply noexcept(false) and add another new symbol to the mangling for noexcept(true). This is going to be useful when working with templates as now comparing function type and noexcept specifier should be done seperatly. What I basically mean is this: int func(

Is it possible to use anonymous classes in C++?

五迷三道 提交于 2019-12-21 03:14:10
问题 I have seen anonymous classes in C++ code on Quora. It's successfully compiled and run. Code here: #include <iostream> auto func() { class // no name { public: int val; } a; a.val = 5; return a; } int main() { std::cout << func().val << std::endl; return 0; } So, Is it valid in C++? Also, I am curious to know, Is it possible to use anonymous classes in C++? 回答1: In C++, an anonymous union is a union of this form: union { ... } ; It defines an unnamed object of an unnamed type. Its members are

Can we use the return value optimization when possible and fall back on move, not copy, semantics when not?

丶灬走出姿态 提交于 2019-12-20 23:19:12
问题 Is it possible to write C++ code where we rely on the return value optimization (RVO) when possible, but fall back on move semantics when not? For example, the following code can not use the RVO due to the conditional, so it copies the result back: #include <iostream> struct Foo { Foo() { std::cout << "constructor" << std::endl; } Foo(Foo && x) { std::cout << "move" << std::endl; } Foo(Foo const & x) { std::cout << "copy" << std::endl; } ~Foo() { std::cout << "destructor" << std::endl; } };