Questions tagged [type-traits]
Type traits are syntactic extensions that allow the developer to determine at compile time various characteristics of a type. C++ support is provided by a combination of compiler support and a set of library templates. Access to the type traits is provided via the header <type_traits>
type-traits
1,275
questions
0
votes
3
answers
64
views
how to create a template with both default type argument and variadic type argument? or any work around
I created a trait struct FnOnArg to see if Fn can call on Arg
template<typename Fn, typename Arg, typename=void>
struct FnOnArg: std::false_type {};
template<typename Fn, typename Arg>
...
-1
votes
1
answer
64
views
std::is_same_v weirdly return false when true is expected using CRT pattern
#include <iostream>
#include <type_traits>
template <template <typename,unsigned,typename...>typename interface,typename T,unsigned ChannelN, typename...Args>
struct base : ...
2
votes
1
answer
125
views
Why does this static_assert on a pointer to an incomplete type in a templated function apparently work?
In the process of replacing a static_assert in a templated function with a requires statement, we discovered that firstly, the function was occasionally being used on an incomplete type, and that ...
0
votes
1
answer
73
views
Why are trait objects usually used via references (&dyn Trait) or smart Pointers (like Box<dyn Trait>)?
In Rust, why are trait objects usually used via references (&dyn Trait) or smart Pointers (like Box<dyn Trait>)? Does it have to be? Or is it better to use it this way?
2
votes
2
answers
90
views
Why does `std::integral_constant` have a `::type` that refers to itself?
I'm reading the documentation on std::integral_constant, and apparently it includes using type that refers to itself.
template <typename T, T Value>
struct integral_constant
{
using type = ...
-2
votes
2
answers
127
views
How to check whether a function template exists in C++?
Is there a way to check the existence of a function (of a specific signature) regardless of whether it is templated or not?
For example:
template <bool FLAG>
class A
{
public:
template <...
1
vote
2
answers
115
views
Conditionally declare a variable of a certain type
Here is what I want to achieve:
struct A { A(/* ... */) {} A(A const&) = delete; };
struct B { B(/* ... */) {} B(B const&) = delete; };
int main()
{
static bool constexpr cond = false;
...
2
votes
1
answer
102
views
How does 'std::add_const' work when I instantiate it with 'const int'? [duplicate]
The following code snippet comes from libstdc++'s implementation of std::add_const,
/// add_const
template<typename _Tp>
struct add_const
{ typedef _Tp const type; };
If I use int to ...
0
votes
3
answers
89
views
How to get the class type of an unnamed struct in a typedef inside the unnamed class?
This is a theoritcal question whether it is possible to get the type of the unnamed struct inside its definition.
int main() {
struct {
using ThisType = ???? //what do i put here?
} _;...
0
votes
0
answers
84
views
Trait for a callable class and signature of operator()
Let's say I have a class
template <class T, class Function>
class Integration
{
public:
T operator()(const Function & f, T a, T b) const
{
/// computes the integral of f over ...
2
votes
1
answer
81
views
SFINAE for checking the validity of a complex expression involving copy assignment
Consider the following classes and take specific note of Concrete<T>::copy_assign:
struct TypeErased
{
TypeErased() = default;
TypeErased& operator=(TypeErased const& other) {
...
3
votes
4
answers
151
views
Template specialization using a variable argument
I am trying to wrap a c-style variant in a library. It uses a type enum, which I mean to fill using templates.
A simplified version looks roughly like this:
enum Type
{
Type_Int,
Type_Double,
...
6
votes
1
answer
133
views
How to generalize a template specialization for any variadic template type?
I have the following type function to calculate whether some type T is part of the list of types in an std::tuple:
template<typename T, typename Tuple>
struct IsInTuple;
template<typename T, ...
0
votes
1
answer
53
views
Is there a way to implement std::regular_invocable as a type_trait style function in c++11/c++14?
I'm currently working on an Arduino "gameboy" style project where I'm trying to make a library that will work like a game engine. In the Arduino IDE there is no c++ STL when using an AVR ...
2
votes
1
answer
136
views
What was the problem with std::is_callable?
Since 2017-03-13 std::is_callable is gone from cppreference.com. The last available description of it is from 2016-11-21 on WaybackMachine.
The main difference between std::is_callable and std::...