typetraits

Deprecated std::is_literal_type in C++17

旧城冷巷雨未停 提交于 2020-08-01 09:29:12
问题 According to cppreference, the trait std::is_literal_type is deprecated in C++17. The question is why and what is the preferred replacement for the future to check whether a type is a literal type. 回答1: As stated in P0174: The is_literal type trait offers negligible value to generic code, as what is really needed is the ability to know that a specific construction would produce constant initialization. The core term of a literal type having at least one constexpr constructor is too weak to be

Deprecated std::is_literal_type in C++17

南笙酒味 提交于 2020-08-01 09:25:01
问题 According to cppreference, the trait std::is_literal_type is deprecated in C++17. The question is why and what is the preferred replacement for the future to check whether a type is a literal type. 回答1: As stated in P0174: The is_literal type trait offers negligible value to generic code, as what is really needed is the ability to know that a specific construction would produce constant initialization. The core term of a literal type having at least one constexpr constructor is too weak to be

Reusable member function in C++

烈酒焚心 提交于 2020-07-20 10:05:23
问题 I'm using this member function to get pointer to object: virtual Object* Create() { return new Object(); } It's virtual so I can get pointer to derived objects, now I'm doing it like this: virtual Object* Create() { return new Foo(); } It is working correctly, but I'd like to do something like this to prevent any mistakes and also to make it easier so I don't have to rewrite that function every time I make new class: virtual Object* Create() { return new this(); } I was trying to find how to

Reusable member function in C++

馋奶兔 提交于 2020-07-20 10:03:47
问题 I'm using this member function to get pointer to object: virtual Object* Create() { return new Object(); } It's virtual so I can get pointer to derived objects, now I'm doing it like this: virtual Object* Create() { return new Foo(); } It is working correctly, but I'd like to do something like this to prevent any mistakes and also to make it easier so I don't have to rewrite that function every time I make new class: virtual Object* Create() { return new this(); } I was trying to find how to

static_assert inside/outside class definition

时光总嘲笑我的痴心妄想 提交于 2020-06-22 11:10:52
问题 Why does static_assert need to be out side of the class definition? Failing code #include <type_traits> class A { public: A(A&&) noexcept {} static_assert(std::is_nothrow_move_constructible<A>::value, "ERROR"); }; int main() { } Working code #include <type_traits> class A { public: A(A&&) noexcept {} }; static_assert(std::is_nothrow_move_constructible<A>::value, "ERROR"); int main() { } And when is it appropriate to use static_asserts in the definition of a class or struct? 回答1: As far as the

Is it possible to define a callable concept that includes functions and lambdas?

▼魔方 西西 提交于 2020-06-16 19:20:13
问题 I want to define a concept that would accept all callable objects. Here's what I have done so far: template<typename F> concept Func = std::is_function_v<std::remove_pointer_t<std::decay_t<F>>> || (requires (F f) { std::is_function_v<decltype(f.operator())>; }); bool is_callable(Func auto&&) { return true; } bool is_callable(auto&&) { return false; } Yet if I define those: auto f = [](auto a, auto b, auto c, auto d, auto e) { return a * b * c * d * e; }; int g(int a, int b) { return a + b; }