【问题标题】:explicit specialization with concepts具有概念的显式专业化
【发布时间】:2021-09-11 23:01:46
【问题描述】:

我正在尝试使用概念对某些模板方法进行显式特化,但它没有在gccmsvc 上编译,但可以在clang 上编译...谁是对的?

#include <type_traits>

template<typename T> 
concept arithmetic = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;

template<typename T>
void foo(const T &value, int &result);

template<>
void foo(const arithmetic auto &value, int &result) {

}

【问题讨论】:

标签: c++ c++20 c++-concepts


【解决方案1】:

正确的语法是

template<arithmetic T>
void foo(const T &value, int &result){}

void foo(const arithmetic auto &value, int &result){} 

所以没有template&lt;&gt;template&lt;arithmetic T&gt;

【讨论】:

  • const 的位置在第二个选项中具有误导性(在第一个选项中并不理想)。 const 修饰符不在概念上,而是在类型上。我建议把它写成arithmetic auto const &amp;value
【解决方案2】:

显式特化不能有const arithmetic auto隐含的模板参数; Clang 是错误的,可能是因为它忽略了 template&lt;&gt; 而只是像您可能想要的那样重载模板。

【讨论】:

  • 有什么方法可以做我想做的事吗?
  • @VladyslavMozhvylo:您通过删除我说它忽略的template&lt;&gt; 来重载模板。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 2021-02-23
相关资源
最近更新 更多