假设我有一些constexpr函数f:
constexpr int f(int x) { ... }
Run Code Online (Sandbox Code Playgroud)
我在编译时知道一些const int N:
或
#define N ...;
Run Code Online (Sandbox Code Playgroud)
要么
const int N = ...;
Run Code Online (Sandbox Code Playgroud)
根据你的答案需要.
我想要一个int数组X:
int X[N] = { f(0), f(1), f(2), ..., f(N-1) }
Run Code Online (Sandbox Code Playgroud)
这样在编译时评估函数,X中的条目由编译器计算,结果放在我的应用程序映像的静态区域,就像我在X初始化列表中使用整数文字一样.
有什么方法可以写这个吗?(例如,使用模板或宏等)
我有最好的:(感谢Flexo)
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 10;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> constexpr A fs() { return A{{ f(i)... }}; }
template<int...> struct S;
template<int... i> struct S<0,i...>
{ static …Run Code Online (Sandbox Code Playgroud) 这似乎是一个非常简单的问题:如何删除第一个(第n个)类型std::tuple?
例:
typedef std::tuple<int, short, double> tuple1;
typedef std::tuple<short, double> tuple2;
Run Code Online (Sandbox Code Playgroud)
上述操作将转变tuple1为tuple2.可能吗?
我希望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)
问:存在更有效的方法来选择可变参数模板的最后一个参数吗?
我写了(在c ++ 11中)一个可变参数模板constexpr函数,它计算参数类型的最大大小,例如:
maxSizeof<int, char, MyType>()
Run Code Online (Sandbox Code Playgroud)
这工作正常.然后我想有一个可变参数模板类,其中一个字段是一个大小等于maxSizeof()的数组.这也应该正常工作:
template <typename... TypesT>
class Myclass {
uint8_t field[maxSizeOf<TypesT...>()]
}
Run Code Online (Sandbox Code Playgroud)
但我还需要Myclass为每个参数类型声明方法.我通过以下方式使用CRTP:
template <typename... TypesT>
class Myclass;
template <>
class Myclass {
uint8_t field[maxSizeOf<TypesT...>()] // (1) Couldn't do it here as there are no `TypesT`
}
template <typename FirstT, typename... OtherT>
class Myclass<FirstT, OtherT...> : public Myclass<OtherT...> {
public:
virtual void doSomething(FirstT object) = 0;
private:
uint8_t field[maxSizeOf<FirstT, OtherT...>()] // (2) Shouldn't do it here as it will create field for …Run Code Online (Sandbox Code Playgroud) 有没有办法制作一个constexpr无符号整数的循环,它满足constexpr布尔函数给出的一些谓词pred(std::size_t)?
我尝试了很多,特别是使用索引技巧,只是为了发现我的数据太大了,这样它超过了递归模板实例化限制256.我将无法改变这个限制,如果可能的话更改.
正如评论中所提到的,这里有一些我想要实现的伪代码:
template<std::size_t... Is>
struct Sequence{};
template<std::size_t N, std::size_t... Is>
struct SequenceGenerator : SequenceGenerator<N-1, N-1, Is...>
{}; //obviously here it gets too deep into recursion, as mentioned
template<std::size_t... Is>
struct SequenceGenerator<0, Is...> : Sequence<Is...>
{};
template<std::size_t N>
struct MyData
{
std::size_t values[N];
static constexpr std::size_t size()
{ return N; }
};
template<typename Lambda, std::size_t... Is>
constexpr MyData<sizeof...(Is)> MyGen(Sequence<Is...>, Lambda func)
{
if(func(Is)...)
return {{ Is... }};
else
return /*some not-invalidating but current element …Run Code Online (Sandbox Code Playgroud) c++ compile-time template-meta-programming variadic-templates c++11
在这里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;}