这是我目前的代码:
template<int n>
struct N{
static const int k = (n >= 2) ? (1 + N<n-2>::k) : N<0>::k;
};
template<>
struct N<0>{
static const int k = 0;
};
Run Code Online (Sandbox Code Playgroud)
以下编译:
int main(int, char *[])
{
cout << N<2>::k;
getchar();
}
Run Code Online (Sandbox Code Playgroud)
以下内容无法编译:
int main(int, char *[])
{
cout << N<1>::k;
getchar();
}
Run Code Online (Sandbox Code Playgroud)
编译器是否急切地计算?:运算符的正确分支?如果是这样,我怎么能懒得评价呢?
我正在用C++编写一个小型库,我只想在模拟算术类型的情况下实例化模板,我发现了以下问题:
如果我有以下定义Foo:
template<typename T, typename Enable = void>
class Foo;
template<typename T>
class Foo<T, std::enable_if<std::is_arithmetic<T>::value>::type> {
Foo() = default;
Foo( const Foo& ) = default;
~Foo() = default;
template<typename U>
Foo( std::initializer_list<U> list )
{
static_assert(std::is_convertible<U, T>::value, "Must use an initializer list with type convertible to T");
for( std::size_t s = 0; s < 10; ++s )
{
tArray[s] = static_cast<U>(list[s]);
}
}
private:
T tArray[10];
};
Run Code Online (Sandbox Code Playgroud)
我尝试按如下方式初始化它:
int main()
{
Foo<int> test{ {1, 2, 3, 4, 5, …Run Code Online (Sandbox Code Playgroud) 我有这个代码
#include <iostream>
size_t F()
{
return 0;
}
template <class Type, class... NextTypes>
size_t F(const Type& type, const NextTypes&... nextTypes)
{
if (!std::is_const<Type>::value)
return sizeof(type) + F(nextTypes...);
else
return F(nextTypes...);
}
int main()
{
int a = 0;
const int b = 0;
const size_t size = F(a,b);
std::cout << "size = " << size << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图在编译时知道常量参数和非常量参数的总大小.当前输出为8,由于某种原因,编译器认为b不是常量,我使用typeid并decltype打印类型a和b输出显示确实b是一个int而不是const int我预期的.我错过了什么?是否有可能将一组可变参数分别为const参数和非const?
以免考虑以下代码:
#include <iostream>
template <typename T_VAL> // not used template argument
struct Foo { int x; };
template <typename T_VAL>
struct Bar { int x; };
template <typename T_VALUE>
struct A
{
// just 2 overloaded data() members:
virtual void data(Foo<T_VALUE>) {
std::cout << "FOO EMPTY\n";
}
virtual void data(Bar<T_VALUE>) {
std::cout << "BAR EMPTY\n";
}
};
template <typename T_VALUE>
struct B : public A<T_VALUE>
{
void data(Foo<T_VALUE>) {
std::cout << "smart FOO impl\n";
}
};
int main(void) {
B<float> …Run Code Online (Sandbox Code Playgroud) 我有一对begin()/ end()方法声明如下:
template <typename... Ts>
Iterator begin(Ts... indices) const;
template <typename... Ts>
Iterator end(Ts... indices) const;
Run Code Online (Sandbox Code Playgroud)
从逻辑上讲,end()可以实现begin().具体来说,end(x, y, ..., z)相当于begin(x, y, ..., z + 1).那么,有没有一个干净的方式转x, y, ..., z成x, y, ..., z + 1用indices,这样我就可以实现end()像
template <typename... Ts>
Iterator end(Ts... indices) const {
return begin(whatever to do with indices...);
}
Run Code Online (Sandbox Code Playgroud) 我知道这个问题相当理论化,但我认为如果将占位符定义为模板,例如:
namespace std {
namespace placeholders {
template <size_t> struct placeholder { constexpr placeholder() {}; };
template <size_t N> constexpr placeholder<N> _{};
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
std::bind(foo, std::placeholders::_<1>, std::placeholders::_<2>);
Run Code Online (Sandbox Code Playgroud)
或者对于c ++ 11:
namespace std {
namespace placeholders {
template <size_t> struct _ { };
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
std::bind(foo, std::placeholders::_<1>{}, std::placeholders::_<2>{});
Run Code Online (Sandbox Code Playgroud)
代码不会丢失任何清晰度,我们可以使用它做一些奇特的元编程.那么......为什么不std::bind使用非类型模板参数实现占位符?
是否可以创建某种std :: enable_if_and_else,如std :: conditional,但没有针对未定义类的编译时错误。
这是一个例子:
static constexpr bool myExpr = true;
struct A {};
struct B;
struct C :
std::conditional<myExpr,
A,
B>::type
{}; // Compilation error: B is declared but not defined
struct D :
enable_if_else<myExpr,
A,
B>::type
{}; // It works
Run Code Online (Sandbox Code Playgroud)
谢谢
继续我读取range-v3库,我意识到关于模板类型的有效表达式的所有检查都有一个拖尾",42"表达式,我想知道它的目的是什么.例如:
namespace concepts {
constexpr struct valid_expr
{
template<typename... T>
void operator()(T&&...) const;
};
}
struct ExplicitlyConvertibleTo
{
template<typename From, typename To>
auto requires_(From (&from)()) -> decltype(
concepts::valid_expr(
((void) static_cast<To>(from()), 42)
));
};
Run Code Online (Sandbox Code Playgroud)
我理解该实现的一些要点,比如强制使用逗号运算符的内括号,避免逗号运算符的一些重载等的void-casting,但为什么不只是简单地写一些像?
concepts::valid_expr(static_cast<To>(from()));
Run Code Online (Sandbox Code Playgroud) 我有一组不同类的对象.我想迭代元组并仅在这些类有一个时才调用某个方法.
例如(伪代码):
struct A { int get( ) { return 5; }; };
struct B { };
struct C { int get( ) { return 10; }; };
int i = 0;
tuple<A, B, C> t;
for ( auto t_element : t )
{
if constexpr ( has_get_method( decltype(t_element) ) )
{
i += t_element.get( );
}
}
Run Code Online (Sandbox Code Playgroud)
我已经知道如何迭代元组并检查一个类是否有一些使用sfinae的方法但是如何跳过没有所需方法的对象?
特别是我正在寻找像这样的结构:
template<class T>
struct tag {
using type = T;
};
Run Code Online (Sandbox Code Playgroud)
这可以用于为构造函数提供模板参数.