Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以为运算符<;模板运算符重载&书信电报;_C++ - Fatal编程技术网

C++ 是否可以为运算符<;模板运算符重载&书信电报;

C++ 是否可以为运算符<;模板运算符重载&书信电报;,c++,C++,我有这两个重载运算符是的,您可以使用函数模板和std::enable_if,以便使用一个既能与glm::vec3一起工作又能与glm::ivec3一起工作的函数: template <typename T> auto operator<<(std::ostream& os, const std::vector<T>& vertices) -> std::enable_if_t< std::is_sam

我有这两个重载运算符是的,您可以使用函数模板和
std::enable_if
,以便使用一个既能与
glm::vec3
一起工作又能与
glm::ivec3一起工作的函数:

template <typename T>
auto operator<<(std::ostream& os, const std::vector<T>& vertices)
    -> std::enable_if_t<
            std::is_same<T, glm::vec3>::value || std::is_same<T, glm::ivec3>::value,
            std::ostream&>
{
  for (auto i = vertices.begin(); i != vertices.end(); ++i){
    os << glm::to_string(*i) << '\n';
  }

  return os;
}

是的,您可以使用函数模板和
std::enable_if
,以使单个函数既能与
glm::vec3
一起工作,又能与
glm::ivec3
一起工作:

template <typename T>
auto operator<<(std::ostream& os, const std::vector<T>& vertices)
    -> std::enable_if_t<
            std::is_same<T, glm::vec3>::value || std::is_same<T, glm::ivec3>::value,
            std::ostream&>
{
  for (auto i = vertices.begin(); i != vertices.end(); ++i){
    os << glm::to_string(*i) << '\n';
  }

  return os;
}

它们是一样的Oo@amchacon,vec3对ivec3是的。这回答了你想问的问题吗?它们是一样的Oo@amchacon,vec3对ivec3是的。这是否回答了你想问的问题?我喜欢新的C++模板:)@ AcCHACON:这里没有什么新的东西。@ JAROD42:关于STD::Enable?IFIFT?限制在<代码> GLM::toSype字符串(*i)将使函数更通用,BTW。我尝试了你的Woobox示例,得到这个:PROG.cc:14:9:错误:“ISSAMEYVIV”不是“STD”STD的成员::IsSAMEYVIV.STD::ISSAMEVIVI爱新C++模板:“AcChaCon:这里没有新的东西。”JAROD42:关于STD::Enable?IFIFT?限制在<代码> GLM::toShin字符串(*i)
将使函数更通用。顺便说一句,我尝试了您的wandbox示例,得到了以下结果:prog.cc:14:9:error:'is_same_v'不是'std'std::is_same_v | std::is_same_v |
template <typename T>
auto operator<<(std::ostream& os, const std::vector<T>& vertices)
    -> decltype(glm::to_string(std::declval<T>()), std::declval<std::ostream&>())
{ 
    // ... as before
}