标签: template-function

如何在模板中调用类型的析构函数?

例如,我们有一个类似的功能:

template <typename TYPE>
void construct_and_destruct(TYPE & object)
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

我们不能object.Type()和那样调用构造函数和析构函数object.~Type()(现在不正确)(为什么?= C

要调用构造函数,我们可以喜欢new(&object) TYPE()。而且我不知道如何调用析构函数(不存在放置delete)。这该怎么做?

c++ templates constructor destructor template-function

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

使用可变参数模板参数调用零参数模板函数指针?

以下是Functional C++博客上的帖子的代码片段,描述了如何实现通用功能评估.

我的问题是如何声明模板函数指针f像R(C ::*f)()没有参数,仍然可以用Args调用它?

// functions, functors, lambdas, etc.
template<
    class F, class... Args,
    class = typename std::enable_if<!std::is_member_function_pointer<F>::value>::type,
    class = typename std::enable_if<!std::is_member_object_pointer<F>::value>::type
    >
auto eval(F&& f, Args&&... args) -> decltype(f(std::forward<Args>(args)...))
{
    return f(std::forward<Args>(args)...);
}

// const member function
template<class R, class C, class... Args>
auto eval(R(C::*f)() const, const C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

template<class R, class C, class... Args>
auto eval(R(C::*f)() const, C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

// …
Run Code Online (Sandbox Code Playgroud)

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

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

内联模板专业化

如果我有一个标题foo.h,我将其包含在我的项目中,它的所有内容似乎都可以正常工作:

template<typename T>
void foo(const T param) {
    cout << param << endl;
}
Run Code Online (Sandbox Code Playgroud)

但是当我向foo.h添加一个specalization时,我得到一个定义规则(ODR)错误:

template<>
void foo(const bool param) {
    cout << param << endl;
}
Run Code Online (Sandbox Code Playgroud)

显然,我可以通过inline专业化来解决这个问题.我的问题是,为什么我需要?如果模板没有违反ODR,为什么要专业化?

c++ inline one-definition-rule template-specialization template-function

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

C++模板函数线程是否安全?

谷歌搜索没有找到任何东西.它们是在使用点创建的,还是实例之间共享的通用部分?

(模板类相同吗?)

c++ thread-safety template-function

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

尽管使用不存在的成员,但程序编译

template <typename T>
void list<T>::copyAll(const list &l)
{
    if(l.isEmpty()) //if the list-to-copy is empty, we're done
    {
        first = last = NULL;
    }
    else
    {
        node *toFollow = l->yhjfrtydfg;
        node *whatever = l.asfqwejfq3fqh23f8hq23r1h23017823r087q1hef;

        while(toFollow != NULL)
        {
            T *newO = new T(*(toFollow->o));
            T here = *newO;
            insertBack(&here);
            toFollow = toFollow->next;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个程序编译(与程序的其余部分),尽管这两条线node *toFollow = l->yhjfrtydfg;node *whatever = l.asfqwejfq3fqh23f8hq23r1h23017823r087q1hef;明显的疯狂投入.这很奇怪,因为任何其他错误都被抓住了.有帮助吗?

c++ templates compiler-errors template-function

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

带有可变参数模板的标记调度,c ++ 11

我正在尝试使函数接受不同的参数,具体取决于枚举.

// cake.h
#pragma once
#include <utility>

enum class TYPE { CupCake, Jelly, BirthdayCake };

struct cake_tin { /* etc etc */ };

/** Fills in different ingredients for different types of cake */
template <TYPE, typename ...Args>
void fill_tin(cake_tin &tin, Args... args);

/** Bakes a cake */
template <TYPE type, typename ...Args>
void bake_cake(Args&&...args) {
    cake_tin tin;
    fill_tin<type>(tin, std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

.cpp文件包含该函数的特化

// cake.cpp
#include "cake.h"

// If I put this in cake.h, I get multiple definition error (since the …
Run Code Online (Sandbox Code Playgroud)

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

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

如何编写一个模板函数,它接受一个数组和一个指定数组大小的int

对于大学练习,我被要求编写一个模板函数"print();",它带有两个参数,1:一个泛型类型的数组,2:一个指定数组大小的int.然后,该函数应将阵列中的每个项目打印到控制台.我在使用函数参数时遇到了一些麻烦.我目前的代码是:

   template <typename Type>
   Type print (Type a, Type b)
    {
        Type items;
        Type array;
        a = array;
        b = items;

        for (int i = 0; i < items; i++) {
        std::cout << std::endl << "The element of the index " << i << " is " << array << std::endl;
        std::cout << std::endl;
    }
Run Code Online (Sandbox Code Playgroud)

并在main()中:

    print(Array[], 10);
Run Code Online (Sandbox Code Playgroud)

显然将Array作为参数放置并不返回值,所以我不知道还能做什么.有任何想法吗?

c++ arrays function template-function

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

Variadic模板:候选人需要1个参数,0提供(扣除错误)

看看这段代码

template<class T> 
void print(T var)
{
    std::cout << var << " ";
}

template<class... Args> 
void Variadic(Args... args)
{
    print(args...);
}

int main()
{
     Variadic();
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时说:

候选人:模板无效打印(T)

候选人期望1个参数,0提供

他是对的.实际上,我没有在参数包中提供任何参数.

但是,为什么这段代码会编译?

template<class T> 
void print(T var)
{
    std::cout << var << " ";
}

template<class... Args> 
void Variadic(Args... args)
{
    auto x = {0, (print(args), 0)...};
}

int main()
{
     Variadic();
}
Run Code Online (Sandbox Code Playgroud)

我要做的第一件事就是将第一个0推入initializer_list <>

好的,现在让我们继续:编译器看到

(print(args), 0)...
Run Code Online (Sandbox Code Playgroud)

它试图调用print()...哦等待... 参数包是空的,print()函数有1个参数.

为什么它会评估auto x = {0};呢?

为什么编译器没有给我与以前完全相同的错误?

c++ templates metaprogramming template-function variadic-templates

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

为什么模板函数不能编译?

这是一个非常简短的片段,不能用g ++ 4.7.1编译(顺便说一下,它不能用gcc 4.6.3编译).

#include <iostream>

template<typename T>
struct Foo
{
    template<typename U>
    friend std::ostream& operator<<(Foo&, U&);
};

template<typename T, typename U>
std::ostream& operator<<(Foo<T> foo, U& u)
{
    std::cout << u;
    return std::cout;
}

int main()
{
    Foo<int> f;
    f << "bar";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这就是gcc 4.7.1输出(4.6.3几乎相同的东西).

/tmp/ccNWJW6X.o:在函数main': main.cpp:(.text+0x15): undefined reference tostd :: basic_ostream>&operator <<(Foo&,char const(&)[4])'collect2:ld返回1退出状态

有谁能解释为什么?

编辑

我也尝试过clang 3.1,它说的完全一样.

c++ linker gcc templates template-function

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

带向量的 C++ 模板函数

我有以下代码行和编译错误。应该是我对模板函数,或者c++泛型,或者其他什么的理解错误。预先感谢您指出这一点。

\n\n
#include <iostream>\n#include <vector>\n\nusing namespace std;\n\ntemplate <typename T>\nT* find(vector<T> &vec, T value)\n{\n  vector<T>::iterator first = vec.begin();\n  vector<T>::iterator last = vec.end();\n  for(; first != last; first ++)\n      if(*first == value)\n          return first;\n  return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

控制台中的编译错误

\n\n
debug.cpp: In function \xe2\x80\x98T* find(std::vector<T, std::allocator<_CharT> >&, T)\xe2\x80\x99:\ndebug.cpp:9: error: expected `;' before \xe2\x80\x98first\xe2\x80\x99\ndebug.cpp:10: error: expected `;' before \xe2\x80\x98last\xe2\x80\x99\ndebug.cpp:11: error: \xe2\x80\x98first\xe2\x80\x99 was not declared in this scope\ndebug.cpp:11: error: \xe2\x80\x98last\xe2\x80\x99 was not declared in this scope\n
Run Code Online (Sandbox Code Playgroud)\n

c++ generics template-function

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