在模板函数(C ++)中将给定的参数乘以3 [英] Multiply given argument by 3 within a template function (C++)

查看:35
本文介绍了在模板函数(C ++)中将给定的参数乘以3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试相乘"给定的3进制字符串-传递给模板函数.

I am trying to "multiply" a given string by 3 - which is passed to a template function.

我收到错误消息:'初始化'不能从'T'转换为'std :: basic_string< char,std :: char_traits,std :: allocator< char'

I am getting the error message: 'initializing' cannot convert from 'T' to 'std::basic_string<char,std::char_traits,std::allocator<char'

template <typename T>
std::string bythree(T argument) {

  std::string message = "";
  
  if (typeid(argument) == typeid(std::string)) {
    std::string mul_str = argument + argument + argument;
    message = mul_str;
  }
}

当我使用 std :: string消息=参数+参数+参数;

在此背后使用逻辑的任何帮助将不胜感激.

Any help on the logic to use behind this would be greatly appreciated.

推荐答案

无论使用哪种类型,它仍然需要编译.因此,如果传递 int ,它将尝试为该字符串分配一个int并失败.

No matter what type you use, it still needs to compile. So, if you pass an int, it will try to assign an int to that string and fail.

要像您一样进行类型测试,有几种方法.您可以创建适用于所有类型的默认模板版本,以及具有特定类型的非模板重载.如果适用,编译器将更喜欢非模板重载:

To do a type test like you're doing, there's a few ways. You can make a default template version that works for all types, and a non-template overload that has your specific type. The compiler will prefer the non-template overload when applicable:

template <typename T>
std::string bythree(T argument) {
  return "not a string";
}

std::string bythree(std::string argument) {
  return argument + argument + argument;
}

您也可以代替 specialize 模板.您提供了例外"对于特定类型的T:

You can also instead specialize the template. You provide an "exception" for a specific type of T:

template <typename T>
std::string bythree(T argument) {
  return "not a string";
}

template<>
std::string bythree<std::string>(std::string argument) {
  return argument + argument + argument;
}

您可以将 enable_if 与特征类型一起使用,以启用具有特定特征的T类型:

You can use enable_if with type traits to enable types of T with specific traits:

template <typename T, typename std::enable_if<!std::is_same<T, std::string>::value, int>::type = 0>
typename std::string bythree(T argument) {
  return "not a string";
}

template <typename T, typename std::enable_if<std::is_same<T, std::string>::value, int>::type = 0>
typename std::string bythree(T argument) {
  return argument + argument + argument;
}

在C ++ 17中,您可以结合使用类型特征和如果constexpr 在函数内进行类型内联的类型测试,就像您尝试做的那样:

In C++17, you can combine type traits and if constexpr do type testing in-line inside the function like you're trying to do:

template <typename T>
std::string bythree(T argument) {
  if constexpr (std::is_same_v<T, std::string>) {
    return argument + argument + argument;
  } else {
    return "not a string";
  }
}

这在 typeid(T)== typeid(std :: string)不起作用的地方起作用,因为如果constexpr,编译器不会尝试编译的内容如果条件不成立(在编译时评估),则阻止.

This works where typeid(T) == typeid(std::string) doesn't, because the compiler won't try to compile the contents of the if constexpr block if the condition isn't true (evaluated at compile-time).

这篇关于在模板函数(C ++)中将给定的参数乘以3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆