Creating Cartesian Product from Integer Range Template Argument

I am working with a third party library which provides a Numeric class that must be templated with precision and scale arguments. Unfortunately, within the context of the library I am creating, the precision and scale is not known until runtime, so I need to create a runtime mapping of those values to their respective templated functions.

The Numeric library supports up to 38 decimal digits, so in theory I need to create a mapping of the form:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>mapping = {
<38, 38>: []() { // do something with Numeric<38, 38> ... },
<38, 37>: []() { // do something with Numeric<38, 37> ... },
...
<37, 37>: []() { // do something with Numeric<38, 37> ... },
<37, 36>: []() { // do something with Numeric<38, 37> ... },
...
}
</code>
<code>mapping = { <38, 38>: []() { // do something with Numeric<38, 38> ... }, <38, 37>: []() { // do something with Numeric<38, 37> ... }, ... <37, 37>: []() { // do something with Numeric<38, 37> ... }, <37, 36>: []() { // do something with Numeric<38, 37> ... }, ... } </code>
mapping = {
  <38, 38>: []() { // do something with Numeric<38, 38> ... },
  <38, 37>: []() { // do something with Numeric<38, 37> ... },
  ...
  <37, 37>: []() { // do something with Numeric<38, 37> ... },
  <37, 36>: []() { // do something with Numeric<38, 37> ... },
  ...
}

For all precision and scale arguments in the range (0, 38], where scale <= precision

This is the code I’ve created, ignoring the implementation of the std::function map values and using a smaller parameter pack for now:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using funcMapType = std::map<std::pair<int, int>, std::function<void(void)>>;
template <int P, int S, int... Rest> struct NumericCreatorInserter {
static void insert(funcMapType &func_map) {
NumericCreatorInserter<P, P>::insert(func_map);
NumericCreatorInserter<P, S>::insert(func_map);
NumericCreatorInserter<P, Rest...>::insert(func_map);
NumericCreatorInserter<S, Rest...>::insert(func_map);
}
};
template <int Precision, int Scale>
struct NumericCreatorInserter<Precision, Scale> {
static void insert(funcMapType &func_map) {
std::cout << "Precision is: " << Precision << " and scale is: " << Scale
<< std::endl;
func_map.emplace(std::make_pair(Precision, Scale), [](void) { return; });
}
};
static funcMapType initializeNumericCreatorMap() {
funcMapType numeric_creators;
NumericCreatorInserter<3, 2, 1, 0>::insert(numeric_creators);
return numeric_creators;
};
</code>
<code>using funcMapType = std::map<std::pair<int, int>, std::function<void(void)>>; template <int P, int S, int... Rest> struct NumericCreatorInserter { static void insert(funcMapType &func_map) { NumericCreatorInserter<P, P>::insert(func_map); NumericCreatorInserter<P, S>::insert(func_map); NumericCreatorInserter<P, Rest...>::insert(func_map); NumericCreatorInserter<S, Rest...>::insert(func_map); } }; template <int Precision, int Scale> struct NumericCreatorInserter<Precision, Scale> { static void insert(funcMapType &func_map) { std::cout << "Precision is: " << Precision << " and scale is: " << Scale << std::endl; func_map.emplace(std::make_pair(Precision, Scale), [](void) { return; }); } }; static funcMapType initializeNumericCreatorMap() { funcMapType numeric_creators; NumericCreatorInserter<3, 2, 1, 0>::insert(numeric_creators); return numeric_creators; }; </code>
using funcMapType = std::map<std::pair<int, int>, std::function<void(void)>>;

template <int P, int S, int... Rest> struct NumericCreatorInserter {
  static void insert(funcMapType &func_map) {
    NumericCreatorInserter<P, P>::insert(func_map);
    NumericCreatorInserter<P, S>::insert(func_map);
    NumericCreatorInserter<P, Rest...>::insert(func_map);
    NumericCreatorInserter<S, Rest...>::insert(func_map);
  }
};

template <int Precision, int Scale>
struct NumericCreatorInserter<Precision, Scale> {
  static void insert(funcMapType &func_map) {
    std::cout << "Precision is: " << Precision << " and scale is: " << Scale
              << std::endl;
    func_map.emplace(std::make_pair(Precision, Scale), [](void) { return; });
  }
};

static funcMapType initializeNumericCreatorMap() {
  funcMapType numeric_creators;
  NumericCreatorInserter<3, 2, 1, 0>::insert(numeric_creators);
  return numeric_creators;
};

Debugging this at runtime, I get the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Precision is: 3 and scale is: 3
Precision is: 3 and scale is: 2
Precision is: 3 and scale is: 3
Precision is: 3 and scale is: 1
Precision is: 3 and scale is: 0
Precision is: 1 and scale is: 0
Precision is: 2 and scale is: 2
Precision is: 2 and scale is: 1
Precision is: 2 and scale is: 0
Precision is: 1 and scale is: 0
</code>
<code>Precision is: 3 and scale is: 3 Precision is: 3 and scale is: 2 Precision is: 3 and scale is: 3 Precision is: 3 and scale is: 1 Precision is: 3 and scale is: 0 Precision is: 1 and scale is: 0 Precision is: 2 and scale is: 2 Precision is: 2 and scale is: 1 Precision is: 2 and scale is: 0 Precision is: 1 and scale is: 0 </code>
Precision is: 3 and scale is: 3
Precision is: 3 and scale is: 2
Precision is: 3 and scale is: 3
Precision is: 3 and scale is: 1
Precision is: 3 and scale is: 0
Precision is: 1 and scale is: 0
Precision is: 2 and scale is: 2
Precision is: 2 and scale is: 1
Precision is: 2 and scale is: 0
Precision is: 1 and scale is: 0

I am expecting to see pairs of (3, 3), (3, 2), (3, 1), (3, 0), (2, 2), (2, 1), etc…

I think I understand the issue with my code – the sequence starting with (3, 3), (3, 1), (3, 0), and (1, 0) happens when P = 3, S = 1, and Rest…=0, which occurs during the first recursive invocation of the function

With that said, I am struggling with how to express what I am trying to achieve in C++. Is there another template specialization I should be introducing to get this to work?

Ideally I am looking for a solution that works with C++17

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật