dan*_*ftk 10 c++ templates template-templates language-lawyer c++11
我有代码可以在遍历字符串容器时找到并打印出模式的匹配项。打印在模板化的函数foo中执行
编码
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <tuple>
#include <utility>
template<typename Iterator, template<typename> class Container>
void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings)
{
for (auto const &finding : findings)
{
std::cout << "pos = " << std::distance(first, finding.first) << " ";
std::copy(finding.first, finding.second, std::ostream_iterator<char>(std::cout));
std::cout << '\n';
}
}
int main()
{
std::vector<std::string> strs = { "hello, world", "world my world", "world, it is me" };
std::string const pattern = "world";
for (auto const &str : strs)
{
std::vector<std::pair<std::string::const_iterator, std::string::const_iterator>> findings;
for (std::string::const_iterator match_start = str.cbegin(), match_end;
match_start != str.cend();
match_start = match_end)
{
match_start = std::search(match_start, str.cend(), pattern.cbegin(), pattern.cend());
if (match_start != match_end)
findings.push_back({match_start, match_start + pattern.size()});
}
foo(str.cbegin(), findings);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时我遇到一个错误,即由于提供的迭代器不一致,类型推导失败,它们的类型变得多种多样。
GCC编译错误:
prog.cpp:35:9: error: no matching function for call to 'foo'
foo(str.cbegin(), findings);
^~~
prog.cpp:10:6: note: candidate template ignored: substitution failure [with Iterator = __gnu_cxx::__normal_iterator<const char *, std::__cxx11::basic_string<char> >]: template template argument has different template parameters than its corresponding template template parameter
void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings)
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
Clang 的输出:
main.cpp:34:9: error: no matching function for call to 'foo'
foo(str.cbegin(), findings);
^~~
main.cpp:9:6: note: candidate template ignored: substitution failure [with Iterator = std::__1::__wrap_iter<const char *>]: template template argument has different template parameters than its corresponding template template parameter
void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings)
Run Code Online (Sandbox Code Playgroud)
我没有抓住什么?从标准的角度来看,我对模板模板类型推断的使用是否错误并且似乎是滥用?无论是G ++ - 9.2与listdc ++ 11也不铛++用的libc ++能够编译此。
son*_*yao 12
从 C++17 开始,您的代码应该可以正常工作。(它用gcc10编译。)
模板模板参数std::vector
有两个模板参数(第二个有默认参数std::allocator<T>
),但模板模板参数Container
只有一个。由于C ++ 17(CWG 150),默认模板参数所允许的模板的模板参数以匹配用较少模板参数模板的模板参数。
Run Code Online (Sandbox Code Playgroud)template<class T> class A { /* ... */ }; template<class T, class U = T> class B { /* ... */ }; template<template<class> class P> class X { /* ... */ }; X<A> xa; // OK X<B> xb; // OK in C++17 after CWG 150 // Error earlier: not an exact match
在 C++17 之前,您可以使用模板模板参数的默认参数定义第二个模板参数Container
,例如
template<typename Iterator, template<typename T, typename Alloc=std::allocator<T>> class Container>
void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings)
Run Code Online (Sandbox Code Playgroud)
或者应用参数包。
template<typename Iterator, template<typename...> class Container>
void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings)
Run Code Online (Sandbox Code Playgroud)