typetraits

Type trait for aggregate initializability in the standard library?

守給你的承諾、 提交于 2021-02-18 22:25:34
问题 The C++ standard library has std::is_constructible<Class, T...> to check if a class can be constructed from the given types as arguments. For example, if I have a class MyClass which has a constructor MyClass(int, char) , then std::is_constructible<MyClass, int, char>::value will be true . Is there a similar standard library type trait that will check that aggregate initialization works, i.e. MyClass{int, char} is well-formed and returns a MyClass ? My use case: I want to write a function

How to check if type is explicitly/implicitly constructible?

喜夏-厌秋 提交于 2021-02-18 06:25:10
问题 How can it be checked that some type is explicitly (or vice versa implicitly) constructible from other type? Is it any SFINAE trick in this situation? I can write is_explicitly_constructible as a combination of std::is_constructible and std::is_convertible: #include <type_traits> template <typename Type, typename Argument> struct is_explicitly_constructible : std::bool_constant < std::is_constructible<Type, Argument>::value && !std::is_convertible<Argument, Type>::value > { }; But do I take

Idiomatic way to write concept that says that type is a std::vector

不羁的心 提交于 2021-02-07 20:39:14
问题 I have the following code that implements following type traits: that type is std::vector that type is std::vector of ints It works but it is quite verbose. Is there a shorter/nicer way to write this using concepts? I know I can steal concepts from range-v3 or some other similar library, but let's assume I want to implement it myself. #include <iostream> #include <string> #include <type_traits> #include <vector> template <class T> struct is_vector { static constexpr bool value = false; };

Could type traits be restricted to not accept other type traits as arguments?

泪湿孤枕 提交于 2021-02-07 07:17:22
问题 Question may be weird so here is a brief motivational example: #include <vector> #include <type_traits> template <typename T> // workaround for gcc 8.3 where volatile int is not trivially copyable using is_tc = std::is_trivially_copyable<std::remove_cv<T>>; // static assert passes compile, oops static_assert(is_tc<std::vector<int>>::value); As you can see mistake is that I have passed the type trait itself to another type trait instead of passing ::type or using std::remove_cv_t . Obvious

Could type traits be restricted to not accept other type traits as arguments?

假装没事ソ 提交于 2021-02-07 07:14:52
问题 Question may be weird so here is a brief motivational example: #include <vector> #include <type_traits> template <typename T> // workaround for gcc 8.3 where volatile int is not trivially copyable using is_tc = std::is_trivially_copyable<std::remove_cv<T>>; // static assert passes compile, oops static_assert(is_tc<std::vector<int>>::value); As you can see mistake is that I have passed the type trait itself to another type trait instead of passing ::type or using std::remove_cv_t . Obvious

How to check for the existence of a subscript operator?

南笙酒味 提交于 2021-02-06 18:47:07
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

How to check for the existence of a subscript operator?

落爺英雄遲暮 提交于 2021-02-06 18:44:43
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

How to check for the existence of a subscript operator?

早过忘川 提交于 2021-02-06 18:40:20
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

static_assert if expressions is constexpr

不问归期 提交于 2021-02-06 09:44:29
问题 I want to create a class template template <class T> class X { // here I'll use T::value (among other things) }; T::value will often be a constexpr static variable, but not always. T::value has to be positive value, so I want to let people know it during compilation, when possible. If T::value was always constexpr, I'd add static_assert like static_assert(T::value > 0, "need positive number"); Is it possible to add this static_assert only for cases when T::value is constexpr? 回答1: We can

What kind of “Traits” are used/defined in the C++0x Standard

拜拜、爱过 提交于 2021-02-05 20:04:43
问题 A trait in C++ encapsulates a family of operations that allow an Algorithm or Data Structure to operator with that type with which it is instantiated. char_traits are an example for grouping string - and file-required functions. But not all traits have "trait" in their name, right? numeric_limits comes to mind. Is this a "Trait", too? Even without the name "trait" in it? So, are there other Templates that could/should be considered a "Trait"? Besides the examples I found: allocator_traits how