标签: class-template

在依赖范围内调用静态函数模板

假设我template<int I> void ft()在结构模板中有一个静态函数模板template<bool B> S,我想ft从另一个函数模板调用template<bool B> void g(),将bool模板参数传递gS:

template<bool B>
struct S {
  static void f() {
  }
  template<int I>
  static void ft() {
  }
};

template<bool B>
void g() {
  S<B>::f();
  S<B>::ft<12>();
}

int main() {
  g<true>();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在GCC 4.5.2中进行编译会给出关于该行的两个错误S<B>::ft<12>():

  1. 在')'令牌之前预期的主要表达
  2. 类型'<unresolved overloaded function type>'和'int'到二进制'operator <'的无效操作数

Comeau(http://www.comeaucomputing.com/tryitout/),在严格的C++ 03模式下,也抱怨该行,说明"预期的表达式",在右括号下面有一个插入符号.S<B>::f()然而,编译器都没有抱怨该行,而且Comeau实际上可以在轻松模式下编译整个最小示例.

如果我删除了g模板,而是像这样实例化S模板参数g:

void …
Run Code Online (Sandbox Code Playgroud)

c++ generic-programming function-templates class-template

3
推荐指数
1
解决办法
1604
查看次数

class <P>在Java中意味着什么?

可能重复:
java中的<TYPE>是什么意思?

您好,我在调试时遇到过这个类,有人可以指点我的意思,请.谢谢.

class Something<P>{
 private P someVariable;
}

//what does <P> mean here? 
Run Code Online (Sandbox Code Playgroud)

谢谢.

java generics class-template

3
推荐指数
1
解决办法
4678
查看次数

具有相关值的模板特化

使用数组大小​​的模板似乎很简单:

template <size_t N>
struct MyArray
{
    char array[N];
};
Run Code Online (Sandbox Code Playgroud)

但是,我想要做的是根据其他内容设置数组大小:

enum MyEnum {Hi, Lo};

template <MyEnum T, size_t N>
struct MyArray
{
    MyEnum type() { return T; }
    char array[N];
};
Run Code Online (Sandbox Code Playgroud)

当MyEnum为Hi时如何将N设置为10,当MyEnum为Lo时如何设置为200?

我想做的就是说

MyArray<Lo> lo; // Result in MyArray<Lo, 200>
MyArray<Hi> hi; // Result in MyArray<Hi, 10>
Run Code Online (Sandbox Code Playgroud)

而不是说

MyArray<Lo, 200> lo;
MyArray<Hi, 10> hi;
Run Code Online (Sandbox Code Playgroud)

这可能吗?

c++ templates class-template

3
推荐指数
2
解决办法
90
查看次数

模板类作为模板参数的默认参数

今天我尝试将模板类传递给模板参数.我的模板类std::map有四个模板参数,但最后两个是默认参数.

我能够得到以下代码来编译:

#include <map>

template<typename K, typename V, typename P, typename A,
    template<typename Key, typename Value, typename Pr= P, typename All=A> typename C>
struct Map
{
    C<K,V,P,A> key;
};

int main(int argc, char**args) {
    // That is so annoying!!!
    Map<std::string, int, std::less<std::string>, std::map<std::string, int>::allocator_type, std::map> t;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我不想一直传递最后两个参数.这真是太多了.我如何在这里使用一些默认模板参数?

c++ templates class-template

3
推荐指数
1
解决办法
214
查看次数

使用嵌套模板时如何公开继承的构造函数?

考虑这个简单的类派生自std::variant

#include <string>
#include <variant>

class my_variant : public std::variant<int, std::string>
{
    using variant::variant;
public:
    std::string foo()
    {
        return "foo";
    }
};
Run Code Online (Sandbox Code Playgroud)

请注意,我故意添加using variant:variant以便在派生类中公开基类的构造函数。如果不这样做,类的实例将无法 在没有大量重新定义的情况下my_variant工作。operator=这编译得很好。


现在让我们开始逐步将其变成模板。

template <typename TChr>
class my_variant : public std::variant<int, std::string>
{
    using variant::variant;

public:
    // Changed to templated return type, compiles fine
    std::basic_string<TChr> foo() 
    {
        return "foo";
    }
};
Run Code Online (Sandbox Code Playgroud)

在这里,唯一的变化是我们在方法中使用了模板参数foo()。一切仍然编译良好。

现在这个:

template <typename TChr>
class my_variant : // Changed to templated base class
    public std::variant<int, …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates class-template c++20

3
推荐指数
1
解决办法
78
查看次数

为什么不能部分特化类成员函数?

模板类的成员函数可以完全特化,例如

template<class A>
struct MyClass {
    // Lots of other members
    int foo();
};

template<class A>
MyClass<A>::foo() { return 42; }

template<>
MyClass<int>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)

会编译没有问题。请注意,这foo()不是模板函数,因此这与模板函数专业化无关(我可以理解在那里不允许部分专业化,因为它与重载相结合会变得非常混乱)。在我看来,上面的代码只是以下模板专业化的简写:

template<class A>
struct MyClass {
    // Lots of other members
    int foo();
};

template<class A>
MyClass<A>::foo() { return 42; }

template<>
struct MyClass<int> {
    // Copy all the other members from MyClass<A>
    int foo();
};

template<>
MyClass<int>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)

这样对吗?

在这种情况下,我想知道为什么不允许使用类似速记的部分专业化,即为什么我不能写

template<class …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization member-functions class-template

2
推荐指数
1
解决办法
697
查看次数

引用基类模板成员变量的简单方法

有没有办法在没有基类名称和范围解析运算符的情况下引用基类模板的成员变量?

template<typename D>
struct B0 {
    int value;
};
struct D0: B0<D0> {
    D0() {
        B0<D0>::value = 1; // OK.
        value = 1;         // OK without `B0<D0>::`.
    }
};

template<typename T>
struct B1 {
    T value;
};
template<typename T>
struct D1: B1<T> {
    D1() {
        B1<T>::value = 1; // OK.
        // value = 1; // Compile error without `B1<T>::`.
                      // Compile error: use of undeclared identifier 'value'
                      // `B1<T>::` is tedious everywhere `value` is referenced.
    }
};

template<typename T, typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates base-class class-template name-lookup

2
推荐指数
1
解决办法
117
查看次数

A variable (or an attribute) can be equal to a type?

I wanted to know if a variable can be equal to a type (here it's the magic_type)

#include <typeinfo>

template<typename T>
class C
{
public:
   magic_type t;
   t list;
   T data;

   C(void)
   {
      if (typeid(data) == typeid(int))
         t = float; // so typeid(list) = typedid(float)
      else
         t = int; // and typeid(list) = typedid(int)
   }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates types class-template c++17

2
推荐指数
1
解决办法
88
查看次数

vector 类如何接受多个参数并从中创建一个数组?

矢量示例

vector<int> a{ 1,3,2 }; // initialize vectors directly  from elements
for (auto example : a)
{
    cout << example << " ";   // print 1 5 46 89
}
MinHeap<int> p{ 1,5,6,8 };    // i want to do the same with my custom class   
Run Code Online (Sandbox Code Playgroud)

知道如何在大括号中接受多个参数并形成一个数组吗?

std::vectorclass 用于std::allocator分配内存,但我不知道如何在自定义类中使用它。 VS Code 显示std::allocator

我做了同样的事情,但它不像那样工作

template<typename T>
class MinHeap
{
    // ...
public:
    MinHeap(size_t size, const allocator<T>& a)
    {
        cout << a.max_size << endl;
    }
    // ...
};
Run Code Online (Sandbox Code Playgroud)

菜鸟在这里....

c++ stdvector class-template c++11 stdinitializerlist

2
推荐指数
1
解决办法
136
查看次数

是否有一个函数可以调用 template&lt;&gt; 中的类名而无需自己键入类名?

我正在尝试获取类的类型名称。当然,我可以只使用typename(class),但我需要将它放在这样的模板中

ClassA<typeid(class).name()> a = ClassA<typeid(class).name()>(args);
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

template<class T>
class A
{
public:
   T t; // the class

   A(T t1)
   {
      t = t1;
   }
};

class B // example class
{
public:
   int i;

   B(int i1)
   {
      i = i1;
   }
};

int main()
{
   B b = B(1);
   A<typeid(b).name()>(b) a = A<typeid(b).name()>(b); // my issue
}
Run Code Online (Sandbox Code Playgroud)

但它总是给我一个错误。

'A': invalid template argument for 'T', type expected.
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题(类A需要能够接受一个属性中的任何类)?

c++ templates class-template c++11 template-argument-deduction

2
推荐指数
1
解决办法
126
查看次数