我希望variadic模板参数必须唯一.我知道多继承时,不允许相同的类继承.
struct A{};
struct B: A, A{}; // error
Run Code Online (Sandbox Code Playgroud)
使用这个规则,我做了一些代码.
#include <type_traits>
template< class T> struct id{};
template< class ...T> struct base_all : id<T> ... {};
template< class ... T>
struct is_unique
{
template< class ... U>
static constexpr bool test( base_all<U...> * ) noexcept { return true; }
template< class ... U>
static constexpr bool test( ... ) noexcept { return false;}
static constexpr bool value = test<T...>(0);
};
int main()
{
constexpr bool b = is_unique<int, float, double>::value; …Run Code Online (Sandbox Code Playgroud) 我知道如何选择可变参数模板的第一个参数:
template< class...Args> struct select_first;
template< class A, class ...Args> struct select_first<A,Args...>{ using type = A;};
Run Code Online (Sandbox Code Playgroud)
这很简单.但是,select_last不相似:
template< class ...Args> struct select_last;
template< class A> struct select_last<A> { using type = A; };
template< class A, class Args...> struct select_last<A,Args...>{
using type = typename select_last<Args...>::type;
};
Run Code Online (Sandbox Code Playgroud)
该解决方案需要深度递归模板即时.我尝试用以下方法解决这个问题:
template< class A, class Args...>
struct select_last< Args ... , A>{ using type = A; }; // but it's not compiled.
Run Code Online (Sandbox Code Playgroud)
问:存在更有效的方法来选择可变参数模板的最后一个参数吗?
我有兴趣递归lambda实现,并发现此代码用于Fibonacci计算:
std::function<int(int)> lfib = [&lfib](int n) {return n < 2 ? 1 : lfib(n-1) + lfib(n-2);};
Run Code Online (Sandbox Code Playgroud)
我有一个问题:std::function是一个多态函数,所以lfib创建/并将lambda保存在堆内存中,而不是堆栈中.因此可能失去该计划的优化可能性.正确与否?
我正在学习C++ 11,并对用户定义的文字感兴趣.所以我决定玩一下.有些语言的语法如下:
int n = 1000_000_000;
Run Code Online (Sandbox Code Playgroud)
我试图在C++ 11中模拟这个功能.
inline constexpr unsigned long long operator "" _000 (unsigned long long n)noexcept
{
return n * 1000;
}
inline constexpr unsigned long long operator "" _000_000 (unsigned long long n)noexcept
{
return n * 1000*1000;
}
inline constexpr unsigned long long operator "" _000_000_000 (unsigned long long n)noexcept
{
return n * 1000*1000*1000;
}
int main(){
constexpr auto i = 100_000; // instead of 100000
constexpr auto j = 23_000_000; // instead …Run Code Online (Sandbox Code Playgroud) 对于远程整数序列,C++ 11没有基于范围的循环.
for(auto e : {0..10} ) // wouldn't compile!!!
Run Code Online (Sandbox Code Playgroud)
所以我决定模拟它.
template< class T , bool enable = std::is_integral<T>::value >
struct range_impl
{
struct iterator
{
constexpr T operator * ()const noexcept { return value; }
iterator& operator ++()noexcept { ++value; return *this; }
friend
constexpr bool operator != (const iterator & lhs, const iterator rhs ) noexcept
{
return lhs.value != rhs.value;
}
T value;
};
constexpr iterator begin()const noexcept { return { first }; }
constexpr iterator end …Run Code Online (Sandbox Code Playgroud) 为什么constexpr变量的decltype失败?
#include <cstdint>
#include <type_traits>
constexpr uint16_t foo(){ return 0;}
constexpr auto cv = foo();
auto v = foo();
static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed
static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success
Run Code Online (Sandbox Code Playgroud) 我想构建函数,如:
template< int ... values>
constexpr bool check( int i ) noexcept
{
switch(i)
{
case values[0]: case values[1]: ... case values[n-1] : // only illustrated.
return true;
default: return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我能做那个功能吗?
更新:谢谢,现在我知道如何实现:
template< int ... values> struct checker;
template< int head, int ... tail> struct checker<head, tail...>
{
static constexpr bool apply( int i ) noexcept {
return i == head || checker<tail...>::apply(i);
}
};
template<> struct checker<>
{
static constexpr bool apply( int ) noexcept { …Run Code Online (Sandbox Code Playgroud) 我可以写作
template< class T0> struct Last0
{
using type = decltype(T0{}); // OK compiles. `type = T0`
};
template< class T0, class T1> struct Last1
{
using type = decltype(T0{}, T1{}); // OK, compiles. `type = T1`
};
template< class T0, class T1, class T2> struct Last3{
using type = decltype(T0{}, T1{}, T2{}); // Ok, compiles. `type = T2`
};
Run Code Online (Sandbox Code Playgroud)
但是,当我使用可变参数模板时,它不会被编译:
template< class ... T> struct Last{
using type = decltype(T{} ... ); //<--- Error !!!
};
Run Code Online (Sandbox Code Playgroud)
什么问题?
我有这样的结构:
struct E1
{
typedef boost::tuple<
boost::optional< N::type_A >, // N - namespace
boost::optional< N::type_B >,
...................
boost::optional< N::type_X > //arbitrary number of, maximal is 7
> data_type;
// for access by name
boost::optional<N::type_A> const& type_A() const { return boost::get<0>(data); }
boost::optional<N::type_B> const& type_B() const { return boost::get<1>(data); }
.....................................
boost::optional<N::type_X> const& type_X() const { return boost::get<2>(data); }
data_type data;
};
Run Code Online (Sandbox Code Playgroud)
问:我如何使用BOOST预处理器创建这个结构?对我来说只知道type_A,type_B,...,type_X类型的名称.
这需要我,因为我必须创建很多这样的结构,只改变type_A,type_B,...类型.
在通常情况下,我可以使用boost预处理器数组还是设置?
这不是秘密,std::get<i>(tuple)让许多程序员感到恼火.
而不是它,我想要使用类似的东西tuple[i].
所以我试着模拟它.
#include <iostream>
#include <type_traits>
#include <tuple>
template < int > struct index{};
template< char ... >
struct combine;
template<> struct combine<> : std::integral_constant< int , 0>{};
constexpr int ten(size_t p)noexcept
{
return p == 0 ? 1 : 10 * ten(p-1);
}
template< char c, char ... t>
struct combine<c, t...> : std::integral_constant< int, (c - '0')*ten(sizeof...(t)) + combine<t...>::value >
{ static_assert(c >= '0' && c <= '9', "only 0..9 digits are allowed"); …Run Code Online (Sandbox Code Playgroud) 在这里http://en.cppreference.com/w/cpp/utility/tuple/tuple_element给出了std :: tuple_element的可能实现.
template< std::size_t I, class T >
struct tuple_element;
// recursive case
template< std::size_t I, class Head, class... Tail >
struct tuple_element<I, std::tuple<Head, Tail...>>
: std::tuple_element<I-1, std::tuple<Tail...>> { };
// base case
template< class Head, class... Tail >
struct tuple_element<0, std::tuple<Head, Tail...>> {
typedef Head type;
};
Run Code Online (Sandbox Code Playgroud)
但是,如果元组有很多参数(超过100或200个参数),这个实现需要深度递归实例化.
Q1:为什么C++ 11没有添加特殊运算符来获取索引元素?像元组[2]或元组[0]?
Q2:有可能减少深度实例化吗?例如,在D语言中,更多模板算法(在typetuple中)需要O(log(N))深度实例化.
编辑:Q1:为什么C++ 11没有添加特殊运算符来从变量模板获取索引元素?like template <class ... T> struct index {typedef T [3] third_element;}