标签: template-templates

C++ 模板模板参数类型推导

我有代码可以在遍历字符串容器时找到并打印出模式的匹配项。打印在模板化的函数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 …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-templates language-lawyer c++11

10
推荐指数
1
解决办法
584
查看次数

是否有功能模板模板参数这样的东西?

所以我知道C++有一个名为"模板模板参数"的功能,您可以将类模板作为模板参数传递.例如:

template <typename T>
class vector { ... };

template <template <typename> class container>  // this is a template template parameter
class foo { ...  };

...

foo<vector> f;  // pass the vector template itself as template parameter
Run Code Online (Sandbox Code Playgroud)

功能模板有类似之处吗?即有没有办法将函数模板(例如std::make_pair)作为模板参数传递给类?

c++ templates template-templates function-template

9
推荐指数
1
解决办法
793
查看次数

模板模板模板参数是扩展还是标准的一部分?

我正在寻找别的东西与模板模板参数和偶然发现这个答案谓其模板模板模板参数标准允许.

但是,以下代码在最新的clang(3.2)和最新的GCC(4.8)中编译:

template<template<template<typename> class> class T> struct test {};
template<template<typename> class T> struct foo {};
test<foo> bar;
Run Code Online (Sandbox Code Playgroud)

这是一个扩展,还是其他答案实际上是错误的并且标准允许?如果没有,是否有任何特殊原因遗漏?

c++ templates template-templates

9
推荐指数
1
解决办法
262
查看次数

模板模板函数参数

对于我的生活,我无法让这个简单的奥术模板魔法工作:

template<typename T, int a, int b>
int f(T v){
  return v*a-b; // just do something for example
}

template<typename T, int a, int b, template<typename,int,int> class func>
class C{
  int f(){
    return func<T,a,b>(3);
  }
};

int main(){
  C<float,3,2, f> c;
}
Run Code Online (Sandbox Code Playgroud)

这可能不涉及仿函数吗?

c++ template-templates template-meta-programming

9
推荐指数
2
解决办法
3533
查看次数

C++ 11模板别名作为模板模板参数导致不同的类型?

我们在编译下面的源代码时发现了一个奇怪的行为:

template<template<class> class TT> struct X { };
template<class> struct Y { };
template<class T> using Z = Y<T>;

int main() {
  X<Y> y;
  X<Z> z;
  z = y; // it fails here
}
Run Code Online (Sandbox Code Playgroud)

这是一个略微修改的示例,取自c ++ 11标准模板别名提案:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf(参见第4页) )另请注意,该提案"声明y和z属于同一类型".因此,在我们的解释中,应该可以从y指定(或复制构造)z.

但是,此代码不使用gcc 4.8.1编译,也不使用clang 3.3编译.这是编译器中的错误还是我们误解了标准?

提前谢谢,craffael等;)

PS Clang错误消息是:

error: no viable overloaded '='

note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'X<template Y>' to 'const X<template Z>' for 1st argument
template<template<class> class TT> struct …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-templates c++11 template-aliases

9
推荐指数
1
解决办法
598
查看次数

模板模板参数可以默认引用其他模板类型参数吗?

我正在尝试做类似以下的事情:

template <typename T>
struct A
{
    template <typename U>
    struct AA
    {
    };
};

template <typename V, template <typename> class W = A<V>::AA> // Causes C3202
struct B
{
};
Run Code Online (Sandbox Code Playgroud)

但Visual Studio 2010编译器吐出:

错误C3202:'AA':模板参数''的默认参数无效,需要一个类模板

如果我用以下模板替换B:

// Replace "T" with "int"
template <typename V, template <typename> class W = A<int>::AA>
struct B
{
};
Run Code Online (Sandbox Code Playgroud)

代码编译得很好,但不是我想要的.如果原始文件不是合法的C++,是否有替代方案为"B"模板的用户提供类似的界面?

c++ templates template-templates

9
推荐指数
1
解决办法
1083
查看次数

在可变参数模板模板参数中使用默认模板参数

我发现下面的最小例子适用于gcc和clang甚至Visual Studio,但它不能用icc编译.我试图确定这是否是有效的C++,但我无法找到回答我的问题的标准的相关部分,因为这是几个不同的概念组合.

// struct with multiple template parameters
template<typename A, typename B = int>
struct C
{

};

// struct that tries to use C's default second parameter without specifying it
template<typename D, template<typename E, typename ...> class F>
struct G
{
  F<D> h;
};

int main()
{
  G<char, C> i;
}
Run Code Online (Sandbox Code Playgroud)

使用icc(16.0.3),编译会出现以下错误:

struct.cpp(12): error: too few arguments for template template parameter "F"
    F<D> h;

          detected during instantiation of class "G<D, F> [with D=char, F=C]" at line 17
Run Code Online (Sandbox Code Playgroud)

这是有效的C++吗?

对我来说似乎应该是,因为 …

c++ icc template-templates variadic-templates c++11

9
推荐指数
1
解决办法
592
查看次数

变量模板模板?

假设您有一个元组类型,并且您想要提取其模板参数包以便实例化另一个模板.如果那是一个类型模板,那么我可以有这样的实用程序:

template < typename Tuple, template <typename...> typename What >
struct PutTupleInT;

template < typename... Types, template <typename...> typename What >
struct PutTupleInT<std::tuple<Types...>, What>
{
    using Result = What<Types...>;
};
Run Code Online (Sandbox Code Playgroud)

但是,如果所需模板是可变模板呢?虽然template <typename...> typename What是类型模板的"占位符",但是变量模板的"占位符"是什么?

我已经尝试了以下forng-4.0.0(现在唯一支持自动类型的非类型模板参数的编译器),但它失败了.实际上我不确定这是否是C++ 17的正确语法.

template < typename Tuple, template <typename...> auto What >
struct PutTupleInV;

template < typename... Types, template <typename...> auto What >
struct PutTupleInV<std::tuple<Types...>, What>
{
    static constexpr auto value = What<Types...>;
};
Run Code Online (Sandbox Code Playgroud)

c++ template-templates variable-templates c++14 c++17

9
推荐指数
1
解决办法
307
查看次数

在模板模板参数的情况下,decltype(auto)可转发的参考非类型模板参数是否可转发

又一个decltype(auto)模板模板参数问题.这次我能够创建的最小代码重现错误如下所示:

template <template <decltype(auto)> class TT, decltype(auto) V>
void foo(TT<V>) {
};

template <decltype(auto)>
struct Bar{};

int x;

int main() {
    foo(Bar<(x)>{});
}
Run Code Online (Sandbox Code Playgroud)

这在[clang]中导致:

prog.cc:11:5: error: no matching function for call to 'foo'
    foo(Bar<(x)>{});
    ^~~
prog.cc:2:6: note: candidate template ignored: substitution failure [with TT = Bar]: non-type template argument is not a constant expression
void foo(TT<V>) {
     ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

[gcc]接受代码.

根据我的理解,代码格式正确,并且clang在解释方面存在错误,但在向lvvm提交错误之前需要确认.我对吗?

c++ templates template-templates language-lawyer c++17

9
推荐指数
1
解决办法
162
查看次数

传递模板模板参数而不指定具体类型

我想实现一个函数调用的包装器,它会做这样的事情:

template <template<class> class F>
void Wrapper(int n, F&& f)
{
    switch (n)
    {
        case 1:
            f<std::int8_t>();
            break;
        case 2:
            f<std::int16_t>();
            break;
        default:
            break;
    }
}

template <class T>
void func()
{
    // ... body of func()
}
Run Code Online (Sandbox Code Playgroud)

这样我就可以在代码中进行以下调用:

Wrapper(1, func);
Run Code Online (Sandbox Code Playgroud)

但是上面的代码没有编译,因为F&& f构造是无效的 - 我需要指定参数的具体类型.但是,如果我使函数签名如下:

template <template<class> class F, class T>
void Wrapper(int n, F<T>&& f)
Run Code Online (Sandbox Code Playgroud)

然后我必须使用以下具体类型进行调用f:

Wrapper(1, func<std::int8_t>);
Run Code Online (Sandbox Code Playgroud)

我将无法切换Wrapper.

我该如何实现我需要的行为?

c++ templates template-templates

9
推荐指数
2
解决办法
752
查看次数