标签: function-templates

确定功能模板中的类型

我想请教您关于功能模板的建议.我有一个功能,可以将一些数据添加到缓冲区.但我还需要将有关数据类型的信息添加到缓冲区中.数据类型是以下枚举:

enum ParameterType
{
   UINT,
   FLOAT,
   DOUBLE
};
Run Code Online (Sandbox Code Playgroud)

我需要从这样的函数创建一个函数模板:

void SomeBuffer::append( double par )
{
    appendType( DOUBLE );
    memcpy( pStr + _length, &par, sizeof( double ) );
    _length += sizeof( double );
    appendType( DOUBLE );
}
Run Code Online (Sandbox Code Playgroud)

你能否告诉我如何根据参数类型从ParameterType传递appendType()的值.

template<class T>
void SomeBuffer::append( T par )
{    
    appendType( ??? );
    memcpy( pStr + _length, &par, sizeof( T ) );
    _length += sizeof( T );
    appendType( ??? );
}
Run Code Online (Sandbox Code Playgroud)

我尝试用一​​些宏来做但没有成功.非常感谢您的任何建议.

c++ macros templates function-templates

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

为什么GCC不允许我将模板参数用于另一个模板的参数?

我编写了以下模板函数来汇总std :: vector对象的内容.它本身就是一个名为sum.cpp的文件.

#include <vector>

template<typename T>
T sum(const std::vector<T>* objs) {
    T total;
    std::vector<T>::size_type i;
    for(i = 0; i < objs->size(); i++) {
        total += (*objs)[i];
    }
    return total;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译此函数时,G ++会发出以下错误:

sum.cpp: In function ‘T sum(const std::vector<T, std::allocator<_Tp1> >*)’:
sum.cpp:6: error: expected ‘;’ before ‘i’
sum.cpp:7: error: ‘i’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

据我所知,返回此错误的原因是因为std::vector<T>::size_type无法解析为某种类型.这是我唯一的选择std::size_t(如果我理解正确但通常但并非总是相同std::vector<T>::size_type),或者是否有解决方法?

c++ templates stl g++ function-templates

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

如何修改参数包的每个元素,并从那些元素创建一个元组?

我遇到了一个使用可变参数函数模板的问题.我需要检查参数包的每个元素,打包元素,然后将所有打包的元素填充到元组中并返回它.以下是我想做的一般概念(返回类型只是占位符,不确定它们是什么):

template<typename A>
sometype func_helper(A a) {
    //examine a, depending on type, do different stuff with it.
    return modified_a;
}

template<typename... Args>
tuple<sometypes...> func(Args... args) {
    return make_tuple(func_helper...(args));
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c++ templates function-templates variadic-templates c++11

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

如何使用可变参数模板打印出函数的参数?

这个例子使用了一个通用的可变参数模板和函数。我想打印出传递给的参数f

#include <iostream>

template <typename T>
void print(T t) 
{
    std::cout << t << std::endl;
}

template <typename...T>
void f(T &&...args) 
{
    print(args...);
    f(args...);
}

int main() 
{
    f(2, 1, 4, 3, 5);
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

Compilation finished with errors:<br>
source.cpp: In instantiation of '`void f(T ...)` [with `T = {int, int, int, int, int}`]':<br>
source.cpp:16:20: required from here <br>
source.cpp:10:4: error: no matching function for call to '`print(int&, int&, int&, int&, int&)`'<br>
source.cpp:10:4: note: candidate is:<br>
source.cpp:4:6: note: …
Run Code Online (Sandbox Code Playgroud)

c++ templates generic-programming function-templates variadic-templates

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

模板函数实例的地址超过T和T const

是否有可能在以下设置中

template <typename T>
inline void id() {
    //...
}

template <typename T>
bool check() {
    return &id<T> == &id<T const>;
}
Run Code Online (Sandbox Code Playgroud)

check会回来true一些T吗?它取决于内部的工作id吗?标准对此有何看法?

c++ function-pointers instantiation function-templates

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

如何使包装类将其构造函数参数转发给 std::vector 的构造函数?

我有一个wrapper类来包装std::vector<int>数据成员。我希望wrapper的构造函数将其参数转发给 的构造函数vector<int>。我尝试使用可变参数模板构造函数来实现这一点。由于std::vector<int> v{1, 2, 3}是由其std::initializer_listctor构建的,因此我希望在wrapper执行此操作时激活vectorinitializer_listctor

wrapper mywrap{1, 2, 3}.

但我没有得到想要的结果:

#include <iostream>
#include <vector>

struct wrapper {
    std::vector<int> v;
    template <typename ...Tn> wrapper(Tn ...args) : v(args...) {}
};

int main() {
                                                            //----Output----
    wrapper wrap1{ 1 };
    std::cout << wrap1.v.size() << std::endl;               // 1
    std::cout << wrap1.v.at(0) << std::endl;                // 0
    wrapper wrap2{ 1, 2 };
    std::cout << wrap2.v.size() << std::endl;               // …
Run Code Online (Sandbox Code Playgroud)

c++ forwarding vector function-templates variadic-templates

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

使用相同的模板可以获得更多功能

我有类似的东西

template <typename T>
    T func1() { /* ... */ }
template <typename T>
    T func2() { /* ... */ }
// many other functions which use the same template line
Run Code Online (Sandbox Code Playgroud)

如果我试试这个

template <typename T>
    T func1() { /* ... */ }
    T func2() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

我得到编译错误.

是否有可能只编写template一次部件并使代码工作?

c++ templates namespaces class function-templates

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

比较If语句中的数据类型

有没有办法在if语句中测试参数的数据类型?这是一个源代码示例:它不会编译,但它用于表达我的意图.

typedef char cInt[sizeof(int)];
typedef char cFloat[sizeof(float)];
typedef char cDouble[sizeof(double)];

template<typename T>
char* convertToCharStr( const T& t ) {
    if ( t == int ) {
        cInt c = t;
        return c;
     }
     if ( t == float ) {
        cFloat c = t;
        return c;
     }
     if ( t == double ) {
        cDouble c = t;
        return c;
     }
     return nullptr;
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试创建一个模板函数,它可以采用任何默认数据类型,如int,unsigned int,float,double,并根据数据类型,它将在堆栈上创建相应的char []变量.传入的数据类型并将数据存储到char []并将指针返回函数.

我会把它放在它的上面,但作为一个注释,字符数组应该是unsigned char.

c++ if-statement function-templates

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

python中的函数模板

我想知道如何在 python 中使用与模板 < typename T>类似的代码,因为它在 C++ 代码示例中使用:

template <typename T>
unsigned int counting_bit(unsigned int v){
   v = v - ((v >> 1) & (T)~(T)0/3);                           // temp
   v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);      // temp
   v = (v + (v >> 4)) & (T)~(T)0/255*15;                      // temp
   return v;
}
Run Code Online (Sandbox Code Playgroud)

我将如何在 python 中以与 C++ 中提到的相同的方式使用变量 typename 类型转换对象?

c++ python templates function-templates

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

为什么编译器不能推导出模板模板参数?

我想编写一个函数,该函数采用任何类型的通用容器并打印它。
让我们暂时搁置它不适用于某些关联容器并关注这个问题:

template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont)
{
   for (const auto it : cont)
   {
      cout << it << " ";
   }
   cout << endl;
}

int main()
{
   vector<string> v;
   print(v);
}
Run Code Online (Sandbox Code Playgroud)

错误指出:

template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont)
{
   for (const auto it : cont)
   {
      cout << it << " ";
   }
   cout << endl;
}

int main()
{
   vector<string> v;
   print(v);
}
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么编译器不能在这里推断出类型?
即使我明确声明print<vector<string>>(v)

c++ templates template-templates function-templates c++11

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