标签: function-templates

这是什么构造:template <int> void funcName(int i)?

我不小心在编写模板函数专业化时犯了一个错误,结果构造通过了 VS17 编译。(下面包含的代码中的第三个构造)

这是一个有效的构造吗?我该如何调用这个函数?

template <class T> void tempfunc(T t)
{
    cout << "Generic Template Version\n";
}

template <>
void tempfunc<int>(int i) {
    cout << "Template Specialization Version\n";
}

template <int> void tempfunc(int i)
{
    cout << "Coding Mistake Version\n";
}
Run Code Online (Sandbox Code Playgroud)

我无法调用第三个构造。

c++ template-specialization function-templates

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

How to pass any number of arguments to C++ template function?

I have this simple C++ template function. I need to pass any number of arguments of any type to the Method, as shown below. How do I do this?

template <typename T1, typename T2, auto Method>
T1 * wrapObject()
{
    T2 * obj = (*_asposeObj.*Method)(// I would like to pass arguments here);
    ...
}
Run Code Online (Sandbox Code Playgroud)

This is a C++20 project, compiler is g++.

I tried:

template <typename T1, typename T2, auto Method, auto ...Args>
T1 * wrapObject(Args... args)
{
    T2 * …
Run Code Online (Sandbox Code Playgroud)

c++ function-templates variadic-templates

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

如何在模板函数中实现模板类类型?

我想为STL容器设计一个打印功能,包括:std::vector, std::map, std::unodered_map, std::set, std::unordered_set, std::list....

理想的函数如下所示:

template<typename T>
void Show(const T& t)
{
    if (istype(t, std::vector)) 
    {  
        // here is a presudo code
        // print vector
        for (auto i: t) cout << i << endl;
    } 
    else if (istype(t, std::unordered_map)) 
    {
        // print unordered_map
    } 
    else 
    {  }
}
Run Code Online (Sandbox Code Playgroud)

我认为问题发生在 istype()

我知道有一些功能std::is_same可以做到这一点。

但是好像只能操作int或者float不能操作std::vector,你能帮帮忙吗?

c++ templates c++-standard-library function-templates c++17

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

带有折叠表达式的模板会创建不需要的参数副本

以下原型旨在进行同步打印:

#include <iostream>
#include <string>
#include <sstream>
#include <mutex>
#include <Windows.h>    // for OutputDebugString

std::mutex sync_mutex;

template<typename T>
void sync_print_impl(std::ostringstream& str, const T& t)
{
    str << t << " ";
}

template<typename ... Args>
void sync_print_impl(std::ostringstream& str, Args ... args)
{
    str; // prevents unused variable warning when sync_print is called without arguments
    (..., sync_print_impl(str, args));
}

template <typename... Args>
void sync_print(Args... args)
{
    std::ostringstream s;
    sync_print_impl(s, args...);

    {
        std::lock_guard<std::mutex> lg(sync_mutex);
        std::cout << s.str() << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

简单测试即可: …

c++ templates function-templates fold-expression c++17

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

为什么模板函数中的 std::is_array 不区分 int 和 array 类型?

在下面的代码中,我使用模板函数和类型特征来区分整数类型(其他情况)和数组类型。我希望输出分别为intarray,而是分别int int使用 int 类型和数组类型实例化模板函数的两个调用:

这是为什么?

#include <iostream>
#include <array>

template <typename T>
inline static void constexpr SetCoordinates()
{
    if (std::is_array<T>::value)
        std::cout<<"array\n";
    else
        std::cout<<"int\n";
}


int main()
{
 int a = 6;
 std::array<int, 4> arr = {1,2,3,4};
 SetCoordinates<decltype(a)>();
 SetCoordinates<decltype(arr)>();
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits function-templates c++11

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

下面使用“typename”有什么区别?

typename在函数的返回类型之前使用“”与在函数声明中不使用它(如下所示)有什么区别?

如果我们根本不使用它,会有什么不同呢?

template< class T > typename std::remove_reference<T>::type&& move( T&& t );
template< class T > std::remove_reference_t<T>&& move( T&& t ) ;
Run Code Online (Sandbox Code Playgroud)

c++ templates return-type typename function-templates

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

如何使用类模板作为函数参数?

我有一个类的声明

template<int a, int b>
class C {
public:
    array[a][b];
}
Run Code Online (Sandbox Code Playgroud)

我想在这样的函数中使用它作为参数:

bool DoSomeTests(C &c1, C &c2);
Run Code Online (Sandbox Code Playgroud)

但是当我编译时,它告诉我'使用类模板需要模板参数列表.' 我试过了

template<int a, int b>
bool DoSomeTests(C &c1, C &c2);
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的错误.我怎样才能解决这个问题?

c++ templates function-templates

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

关于函数模板的相同代码块在g ++下编译好,但VC6下的错误,为什么?

我正在阅读C++ Primer第3版的 "功能模板"一章,当我试图按照这个例子时,我发现代码几乎与本书在VC6下编译时遇到错误相同但在g ++下一切正常.我不知道为什么?

这是代码:

#include <iostream>
using namespace std;

template<typename T1, typename T2, typename T3>
T1 my_min(T2 a, T3 b)
{
    return a>b?b:a;
}

int main()
{
    int (*fp)(int, int) = &my_min<int>;
    cout<<fp(3,5)<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

发生的错误VC6如下所示:

error C2440: 'initializing' : cannot convert from '' to 'int (__cdecl *)(int,int)'
None of the functions with this name in scope match the target type
Run Code Online (Sandbox Code Playgroud)

c++ function-templates

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

可以在C++中使用缺少的模板参数调用模板函数吗?

这是一个面试问题,已经完成.

哪条线有错误?

  #include<iostream>
  template<class T> void foo(T op1, T op2)
  {
      std::cout << "op1=" << op1 << std::endl;
      std::cout << "op2=" << op2 << std::endl;
  }

  template<class T>
  struct sum
  {
      static void foo(T op1, T op2)
      {
              std::cout << "sum=" << op2 << std::endl ;
      }
  };

  int main()
  {
      foo(1,3);  // line1
      foo(1,3.2);  // line2
      foo<int>(1,3);  // line3
      foo<int>(1, '3') ; // line 4
      sum::foo(1,2) ; // line 5  , 

      return 0;
  }
Run Code Online (Sandbox Code Playgroud)

第2行有错误,因为模板参数与定义不匹配.第5行有错误,因为缺少模板参数.

但是,第1行不是错误,我不知道为什么,它是否也错过了模板参数?

谢谢 !

c++ templates class function-templates

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

64位G ++ 4.6.3不会将long视为专用函数模板中的long,即使它们的大小相同.这是一个错误吗?

请考虑以下代码:

#include <iostream>
#include <cinttypes>  

template<class T>
void f();

template<>
inline void f<long long>() {
  std::cout<<"f<long long>()"<<std::endl;
}

int main(int , char** ) {
  std::cout<<"sizeof(long)="<<sizeof(long)<<std::endl;
  std::cout<<"sizeof(long long)="<<sizeof(long long)<<std::endl;
  f<int64_t>();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

32位G ++ 4.6.3成功编译并生成输出:

sizeof(long)=4
sizeof(long long)=8
f<long long>()
Run Code Online (Sandbox Code Playgroud)

但是,在64位G ++ 4.6.3下进行编译会产生链接器错误:

undefined reference to `void f<long>()'
ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

即使编译并运行f<int64_t>()注释掉的行产生:

sizeof(long)=8
sizeof(long long)=8
Run Code Online (Sandbox Code Playgroud)

有没有一个很好的理由为什么64位G ++对待f<long>f<long long>作为不同的功能,即使long并且long long大小相同,或者这是一个我应该报告的错误?

c++ g++ template-specialization function-templates long-long

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