Извлеките шаблонные параметры C++

Вы можете использовать регулярные выражения, см. https://regex101.com/ :

const regex = /accumulate.*price.*below/gm;
const str = `we could accumulate it at the price below 4000

we could do it at the price below 4000'`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

.

27
задан cdleary 19 November 2008 в 07:30
поделиться

1 ответ

Мне нравится ответ Marc Garcia, потому что он показывает, как извлечь шаблонный параметр универсальным способом, но я полагаю, что его пример может быть более простым:

#include <type_traits>
#include <iostream>

template<int>
struct MyType {};

template<template <int> typename T, int N>
constexpr int extract(const T<N>&) { return N; }

int main() {
    constexpr MyType<5> myObj;
    std::cout << extract(myObj);
}

Живой демонстрационный

0
ответ дан 28 November 2019 в 05:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: