久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++11:數組的編譯時間計算

C++11: Compile Time Calculation of Array(C++11:數組的編譯時間計算)
本文介紹了C++11:數組的編譯時間計算的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

假設我有一些 constexpr 函數 f:

Suppose I have some constexpr function f:

constexpr int f(int x) { ... }

而且我在編譯時知道一些 const int N:

And I have some const int N known at compile time:

要么

#define N ...;

const int N = ...;

根據您的回答的需要.

我想要一個 int 數組 X:

I want to have an int array X:

int X[N] = { f(0), f(1), f(2), ..., f(N-1) }

這樣函數在編譯時被評估,X 中的條目由編譯器計算,結果被放置在我的應用程序映像的靜態區域中,就像我在我的 X 初始值設定項列表中使用了整數文字一樣.

such that the function is evaluated at compile time, and the entries in X are calculated by the compiler and the results are placed in the static area of my application image exactly as if I had used integer literals in my X initializer list.

有什么辦法可以寫這個嗎?(例如模板或宏等)

Is there some way I can write this? (For example with templates or macros and so on)

我最好的:(感謝 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 constexpr A gs() { return fs<0,i...>(); } };

template<int i, int... j> struct S<i,j...>
{ static constexpr A gs() { return S<i-1,i,j...>::gs(); } };

constexpr auto X = S<N-1>::gs();

int main()
{
        cout << X[3] << endl;
}

推薦答案

有一個純 C++11(沒有 boost,也沒有宏)解決這個問題.使用與 this answer 相同的技巧,我們可以構建一個數字序列并將它們解包以調用 f 構造一個 std::array:

There is a pure C++11 (no boost, no macros too) solution to this problem. Using the same trick as this answer we can build a sequence of numbers and unpack them to call f to construct a std::array:

#include <array>
#include <algorithm>
#include <iterator>
#include <iostream>

template<int ...>
struct seq { };

template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };

template<int ...S>
struct gens<0, S...> {
  typedef seq<S...> type;
};

constexpr int f(int n) {
  return n;
}

template <int N>
class array_thinger {
  typedef typename gens<N>::type list;

  template <int ...S>
  static constexpr std::array<int,N> make_arr(seq<S...>) {
    return std::array<int,N>{{f(S)...}};
  }
public:
  static constexpr std::array<int,N> arr = make_arr(list()); 
};

template <int N>
constexpr std::array<int,N> array_thinger<N>::arr;

int main() {
  std::copy(begin(array_thinger<10>::arr), end(array_thinger<10>::arr), 
            std::ostream_iterator<int>(std::cout, "
"));
}

(使用 g++ 4.7 測試)

(Tested with g++ 4.7)

你可以完全跳過 std::array 多做一點工作,但我認為在這種情況下,只使用 std::array 更干凈、更簡單.

You could skip std::array entirely with a bit more work, but I think in this instance it's cleaner and simpler to just use std::array.

您也可以遞歸執行此操作:

You can also do this recursively:

#include <array>
#include <functional>
#include <algorithm>
#include <iterator>
#include <iostream>

constexpr int f(int n) {
  return n;
}

template <int N, int ...Vals>
constexpr
typename std::enable_if<N==sizeof...(Vals),std::array<int, N>>::type
make() {
  return std::array<int,N>{{Vals...}};
}

template <int N, int ...Vals>
constexpr
typename std::enable_if<N!=sizeof...(Vals), std::array<int,N>>::type 
make() {
  return make<N, Vals..., f(sizeof...(Vals))>();  
}

int main() {
  const auto arr = make<10>();
  std::copy(begin(arr), end(arr), std::ostream_iterator<int>(std::cout, "
"));
}

可以說更簡單.

這篇關于C++11:數組的編譯時間計算的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 欧美成人自拍视频 | 欧美二区在线 | 三级成人在线观看 | 国产精品夜夜夜一区二区三区尤 | 盗摄精品av一区二区三区 | 综合五月婷 | 国产目拍亚洲精品99久久精品 | 免费看啪啪网站 | 91精品国产91久久久久久吃药 | 久久精品高清视频 | 国产视频在线一区二区 | av大片| 欧美一级毛片在线播放 | 日韩精品一区二区三区中文在线 | 午夜爽爽爽男女免费观看 | 国产精品久久久久久一区二区三区 | 中文字幕在线免费视频 | 第一av| 国产精品久久久久久久久久久久久久 | 成人影院免费视频 | 日韩精品二区 | 日韩影音| 亚洲精品视频在线播放 | 欧美 日韩精品 | 又爽又黄axxx片免费观看 | www.久| 国产精品一区二区三区久久 | 日本一区二区在线视频 | 毛片入口 | 国产福利在线 | 99re视频在线免费观看 | 成人深夜福利在线观看 | 激情视频一区 | 欧美一区 | 国产福利观看 | 亚洲一区二区三区四区五区中文 | 九九视频网 | 午夜视频免费 | 久草新在线 | 91n成人| 亚洲国产aⅴ精品一区二区 免费观看av |