标签: template-templates

功能性C++通过模板滥用

我决定尝试使用模板在C++中编写功能映射实现,这就是我提出的:

template <
    class U, 
    class V, 
    template <class> class T 
>

class T<V> WugMap(
    class T<U>::const_iterator first, 
    class T<U>::const_iterator second, 
    V (U::*method)() const)

{
    class T<V> collection;
    while (first != second)
    {
        collection.insert(collection.end(), ((*(first++)).*method)());
    }
    return collection;
}
Run Code Online (Sandbox Code Playgroud)

现在这一切都很好,花花公子,甚至编译.问题是,我不知道如何实际调用它.

尝试天真的方式会产生以下错误:

prog.cpp:42: error: no matching function for call to 
‘WugMap(__gnu_cxx::__normal_iterator<Container*, std::vector<Container, 
std::allocator<Container> > >, __gnu_cxx::__normal_iterator<Container*, 
std::vector<Container, std::allocator<Container> > >, int (Container::*)()const)’
Run Code Online (Sandbox Code Playgroud)

据我所知,所有论据都是正确的.gcc根本没有提出任何替代方案,这让我相信我对WugMap的定义是可疑的,但它编译得很好,所以我很丢失.任何人都可以引导我度过这种愚蠢吗?

如果有人可以建议一个更好的方法来编写这样的函数来支持使用包含任何类型对象的任何类型的集合,我将考虑更改它.

到目前为止,这是我的想法.

我目前正在使用Ideone,它使用的是C++ 03,gcc 4.3.4.

附录1

这在C++ 11中是否可行?有人暗示它是.我知道C++ 11中的模板支持不同数量的参数,因此我也会修改我的要求以适应这种情况.我会付出一些努力来写一些东西,但与此同时,这里是我正在寻找的要求:

c++ collections templates functional-programming template-templates

5
推荐指数
1
解决办法
654
查看次数

模板模板功能和参数扣除

我遇到了模板模板和参数扣除的问题.这是代码:

template<typename U, template<typename> class T>
void test(T<U>&& t)
{
  ...
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以接受左值和左值,但只适用于右值.折叠规则"T &&& = T&"在这种情况下不适用?

当然,我也可以声明左值引用函数,但是使代码的可读性降低.

如果你问我为什么需要这个就是使用static_assert来检查T是否是一个特定的类.如果有一种更简单的方法,我会很乐意改变我的代码,但我想知道模板模板是否可以这种方式使用.

谢谢

c++ templates rvalue-reference template-templates c++11

5
推荐指数
1
解决办法
144
查看次数

来自不同命名空间的模板模板参数可以成为朋友吗?

如果这个问题的标题不太有用,我道歉; 我不知道在不给出以下示例的情况下提出这个问题的简洁方法:

template <template <class> class Arg>
class C {
    typedef C<Arg> type;
    friend class Arg<type>;
  public:
    C() {
        a_.set(this);
    }
  private:
    int i_;
    Arg<type> a_;
};

template <class Type>
class Arg1 {
  public:
    void set(Type* t) {
        t_ = t;
        t_->i_ = 1;
    }
  private:
    Type* t_;
};

namespace NS {

    template <class Type>
    class Arg2 {
      public:
        void set(Type* t) {
            t_ = t;
            t_->i_ = 2;
        }
      private:
        Type* t_;
    };

}
Run Code Online (Sandbox Code Playgroud)

如你所见,Arg2是一份副本Arg1.但是,VS …

c++ templates template-templates

5
推荐指数
1
解决办法
135
查看次数

从模板化模板类和可变参数模板声明"容器"对象

我需要声明一个可以存储不同类型容器的类.即如果它可以处理std :: bitset和std :: array会很好.但是,这两个类需要不同的模板参数...是否有可能(以及可能,如何)使用模板化模板类和可变参数模板来声明这种类?

示例(但错误):

template<template <typename..., std::size_t> class Container,
         std::size_t N,
         typename... Args>
class Base_Class
{
    ...
    Container<Args..., N/2> container;
};
Run Code Online (Sandbox Code Playgroud)

编译器抱怨N/2不是类型.显然,对于std :: array和std :: bitset,我需要将大小作为最后一个模板参数......是否有可能编码这种疯狂?

谢谢!

编辑:就我而言,主要问题是可变参数模板只能在右侧扩展,因此可变参数必须是最后一个.任何人都知道是否有任何计划允许在C++ 17中使用以下语法?

template<typename... Args, typename T>
struct A
{};
Run Code Online (Sandbox Code Playgroud)

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

5
推荐指数
1
解决办法
418
查看次数

使用 libtooling 获取完全限定的模板模板参数名称

我正在尝试使用模板模板参数libtooling来打印CXXRecordDecl模板类的实例化。不幸的是,模板模板参数的字符串表示不是完全限定的(例如,它缺少命名空间)。

我正在CXXRecordDecl使用以下代码打印:

clang::PrintingPolicy policy = compiler_instance->getLangOpts();
std::string name = decl->getTypeForDecl()->getCanonicalTypeInternal().getAsString(policy);
Run Code Online (Sandbox Code Playgroud)

这是一个示例,我希望输出为ns::A<ns::B>,但我得到ns::A<B>

clang::PrintingPolicy policy = compiler_instance->getLangOpts();
std::string name = decl->getTypeForDecl()->getCanonicalTypeInternal().getAsString(policy);
Run Code Online (Sandbox Code Playgroud)

如何使用模板模板参数打印类的完全限定名称?

在相关说明中,有没有办法在不调用 的情况下做到这一点getCanonicalTypeInternal,这听起来像是一个内部函数?

[编辑 #1]我也试过decl->getQualifiedNameAsString(),它完全省略了模板参数和输出ns::A

[编辑#2] Cling 用一组问题交换另一组问题。它确实为模板模板参数正确生成了完全限定的类型。但是,它会为函数(和函数指针)的参数和返回类型生成非限定名称。例如,下面的代码产生输出ns::A<void (B)>而不是ns::A<void (ns::B)>

namespace ns {

template <template <class> class T>
class A {
  T<int> x;
};

template <class T>
class B {
  T y;
};

} // namespace ns

int main(int argc, …
Run Code Online (Sandbox Code Playgroud)

c++ clang template-templates libtooling

5
推荐指数
1
解决办法
1236
查看次数

如何使用`std :: array`作为`template <typename> class`形式的模板参数?

请考虑以下tree课程

template<typename T, template<typename> class Tuple>
class tree
{
private:
    T m_value;
    Tuple<tree> m_children;
};

template<typename T, std::size_t N>
using static_tree = tree<T, std::array<T, N>>;
Run Code Online (Sandbox Code Playgroud)

这没有明确的定义.std::array<T, N>不适合的模板参数Tuple.我认为这static_tree是明确的意图.我们可以做点什么

template<std::size_t N>
struct helper
{
    template<typename T>
    using type = std::array<T, N>;
};

template<typename T, std::size_t N>
using static_tree = tree<T, helper<N>::template type>;
Run Code Online (Sandbox Code Playgroud)

没有helper班级还有其他选择吗?

c++ templates template-templates c++14

5
推荐指数
2
解决办法
1016
查看次数

在 C++ 中使用模板类的嵌套类作为模板模板参数

在 C++ 中,我想在模板化类中使用嵌套类作为模板模板参数。对于非嵌套类,模式是:

template<class T>
class A {
public:
    T a;
    // ...
};

template<class T, template<class ST> class S>
class B {
public:
    S<T> b;
    // ...
};

B<int, A> b;
Run Code Online (Sandbox Code Playgroud)

现在,我想一个嵌套类添加A和使用嵌套类为模板,模板参数S类的B,就像这样:

template<class T>
class A {
public:
    class AA {
    public:
        T aa;
        // ...
    };
    // ...
};

template<class T, template<class ST> class S>
class B {
public:
    S<T> b;
    // ...
};

B<int, A> b1;          // ok
B<int, A::AA> b2; …
Run Code Online (Sandbox Code Playgroud)

c++ templates inner-classes template-templates

5
推荐指数
2
解决办法
1958
查看次数

将指定的模板类型作为模板参数传递

说我有一些模板类型......

template <typename T> struct Foo {
    Foo(T t) {}
};
Run Code Online (Sandbox Code Playgroud)

有没有办法将指定的Foo类型传递给函数,以便函数可以直接看到T?

理想情况下,我可以写这样的东西......

Foo<int> foo = create<Foo<int>>();
Run Code Online (Sandbox Code Playgroud)

我能来的最接近的是

template <
    template <typename> typename TT,
    typename T,
    std::enable_if_t<std::is_same<TT<T>, Foo<T>>::value, int> = 0
>
Foo<T> create() {
    return Foo<T>(T());
}
Run Code Online (Sandbox Code Playgroud)

然后将使用像

Foo<int> foo = create<Foo, int>();
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

c++ templates template-templates c++14

5
推荐指数
1
解决办法
1244
查看次数

C++模板模板非类型参数

我想要实现以下目标:

template<template<typename> bool Function_, typename ... Types_>
constexpr auto find(Tuple<Types_ ... >) noexcept
{
    // ... 
}
Run Code Online (Sandbox Code Playgroud)

可能的功能可能是:

template<typename T>
inline constexpr bool is_pointer_v = is_pointer<T>::value;
Run Code Online (Sandbox Code Playgroud)

那么find的用法是:

Tuple<int, char, void *> t;
find<is_pointer_v>(t);
Run Code Online (Sandbox Code Playgroud)

不要担心find的实现,我只是询问如何做" template < typename > bool Function_"因为bool当前c ++中的部分无效.

任何帮助表示赞赏!

编辑:

这是一个为什么我不能将" is_pointer" 传递给函数的例子:

template<typename T_>
constexpr auto add_pointer(Type<T_>) noexcept
{ return type_c<T_ *>; }

template<typename F_, typename T_>
constexpr auto apply(F_ f, Type<T_> t) noexcept
{
    return f(t);
}

int main(void)
{
    Type<int> …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-templates non-type c++17

5
推荐指数
1
解决办法
511
查看次数

为什么我不能将std :: unique_ptr用作"template <class> class"参数?

这段代码:

#include <memory>

template <template <typename> class Ptr>
class A { Ptr<int> ints; };

using B = A<std::unique_ptr>;
Run Code Online (Sandbox Code Playgroud)

产生以下错误(使用GCC 6.3):

a.cpp:6:28: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class> class Ptr> class A’
 using B = A<std::unique_ptr>;
                            ^
a.cpp:6:28: note:   expected a template of type ‘template<class> class Ptr’, got ‘template<class _Tp, class _Dp> class std::unique_ptr’
Run Code Online (Sandbox Code Playgroud)

现在,我可以解决这个问题,就像这样:

template <typename T>
using plugged_unique_ptr = std::unique_ptr<T>;
using B = A<plugged_unique_ptr>;
Run Code Online (Sandbox Code Playgroud)

但为什么我要这样做?我的意思是,为什么编译器不愿意"插入"第二个模板参数std::unique_ptr及其默认值并允许std::unique_ptr用作模板参数A

c++ unique-ptr template-templates c++11 template-argument-deduction

5
推荐指数
2
解决办法
642
查看次数