假设您有一个模板参数T
.
有什么区别
add_cv_t<T>
和 const volatile T
add_const_t<T>
和 const T
add_volatile_t<T>
和 volatile T
add_lvalue_reference_t<T>
和 T&
add_rvalue_reference_t<T>
和 T&&
add_pointer_t<T>
和T*
?我为什么要用add_rvalue_reference_t<T>
而不是T&&
举个例子.有什么规则可供选择吗?
我试图在显式和隐式转换构造函数之间切换enable_if
.
我的代码目前看起来像
#include <type_traits>
#include <cstdint>
enum class enabled {};
template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
template <bool B, typename T = void> using disable_if_t = typename std::enable_if<!B, T>::type;
template <std::intmax_t A> struct SStruct
{
static constexpr std::intmax_t a = A;
};
template <typename T> struct SCheckEnable : std::integral_constant<bool, T::a == 0>
{
};
template <typename U, typename T> class CClass
{
public:
template <typename T2, enable_if_t<SCheckEnable<U>::value, enabled>...> constexpr CClass(T2 v) …
Run Code Online (Sandbox Code Playgroud) 我有两个枚举,它们基本上确定(在运行时)要做什么。“映射”看起来像
struct Foo { class CA; class CB; class CC; CA a; CB b; CC c; };
enum Base { A, B, C };
enum Func { X, Y };
Foo foo;
// A, X => use(foo.a.x());
// A, Y => use(foo.a.y());
// B, X => use(foo.b.x());
// B, Y => use(foo.b.y());
Run Code Online (Sandbox Code Playgroud)
问题是,a
、b
和C
,以及x()
和的返回类型y()
都是不同的类型(一些非常巨大的模板类型)。
使用开关或 ifs 映射两个枚举非常难看,并且需要付出很多努力,所以我想知道我是否可以以某种方式编写如下内容:
struct Foo { class CA; class CB; class CC; CA a; CB b; CC …
Run Code Online (Sandbox Code Playgroud) 当我有下面的伪类:
template <class T> class tmplClass
{
void doSomething(T input);
};
Run Code Online (Sandbox Code Playgroud)
有一种方法来改变void doSomething(T input)
到void doSomething(const T& input)
时的sizeof(T)是在较大的系统架构.
tmplClass<char> c;
例如,void doSomething(T input)
当您有tmplClass<[another big class with lots of variables]>
使用时,意味着使用void doSomething(const T& input)
根据cppreference,可以使用定义文字
CSomeClass operator ""s(const char* literal, size_t size);
Run Code Online (Sandbox Code Playgroud)
在阅读完该段后,我认为应该也可以定义
CSomeClass operator ""r(const char* literal, size_t size);
Run Code Online (Sandbox Code Playgroud)
(注意r
ud后缀而不是s
)
重载s
只是给出了clang警告
warning: user-defined literal suffixes not starting with '_' are reserved [-Wuser-defined-literals]
Run Code Online (Sandbox Code Playgroud)
当我正在编译时,我无法理解-std=c++14
.重载r
给出了
error: invalid suffix on literal; C++11 requires a space between literal and identifier [-Wreserved-user-defined-literal]
warning: user-defined literal suffixes not starting with '_' are reserved; no literal will invoke this operator [-Wuser-defined-literals]
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎更不负责任.
为什么clang会发出这些警告/错误,以及如何使r
ud-suffix有效.
我知道有可能像这样对(命名的)构造函数进行模式匹配:
f1 :: Maybe a -> Bool
f1 Nothing = False
f1 (Just x) = True -- in reality have something that uses x here
f2 :: [a] -> Int
f2 [] = False
f2 x = True
Run Code Online (Sandbox Code Playgroud)
我怎样才能为 general Alternative
s编写这样的函数,类似于
f :: (Alternative m) => m a -> Bool
f empty = False
f x = True
Run Code Online (Sandbox Code Playgroud)
如果我尝试这个,我会收到错误Parse error in pattern: empty
。我想这是有道理的,empty
作为这里的函数而不是构造函数。但是我怎样才能按照Alternative
惯用的方式为 general 做到这一点呢?
编辑 1:
我的实际目标是为自定义结果类型定义一个Monad
实例(也可能是一个MonadPlus
实例)。代替基本Either …
我有一个integral_constant
确定类是否在提供的类列表中:
template <typename T> using decay_t = typename std::decay<T>::type;
template <typename... Args> struct SOneOf : std::integral_constant<bool, false>
{
};
template <typename T1, typename T2, typename... Tail> struct SOneOf<T1, T2, Tail...>
: std::integral_constant<bool, SOneOf<T1, T2>::value || SOneOf<T1, Tail...>::value>
{
};
template <typename T1, typename T2> struct SOneOf<T1, T2>
: std::integral_constant<bool, std::is_same<decay_t<T1>, decay_t<T2>>::value>
{
};
template <typename T> struct SOneOf<T> : std::integral_constant<bool, false>
{
};
Run Code Online (Sandbox Code Playgroud)
我也知道我可以将模板化的类作为模板参数通过template <template <typename> class T>
.
假设我有两节课
template <typename T> class CClassA;
template <typename …
Run Code Online (Sandbox Code Playgroud)