【问题标题】:C++ template instantiation with complex type具有复杂类型的 C++ 模板实例化
【发布时间】:2014-12-11 08:23:46
【问题描述】:

我想实例化一个函数,其中模板参数类似于以下complexType

template <class U, class type> struct complexType;
template <class type> struct complexType<double, type>
{
  typedef type T;
};
template <class type> struct complexType<int, type>
{
  typedef type T;
};

我在头文件中得到以下内容

template <typename U>
void XYZ(
  typename complexType<U, double>::T abc);

以及cpp文件中的实现

template <typename U>
void XYZ(
  typename complexType<U, double>::T abc)
{
  abc = 1.0;
}

template
void XYZ(
  complexType<double, double>::T abc);

不幸的是,GCC 给了我以下编译器错误消息

template-id XYZ&lt;&gt; for void XYZ(complexType&lt;double, double&gt;::T) 不匹配任何模板声明

关于如何解决这个问题的任何建议?

【问题讨论】:

  • 你也不能使用template。只使用void XYZ(complexType&lt;double, double&gt;::T abc);
  • @RSahu 更适合简单地超载。 (上升)
  • 这是一个显式实例化,所以这里需要template

标签: c++ templates


【解决方案1】:

U 不是由它的论点推导出来的。你需要一个明确的模板规范:

template
void XYZ<double>(
    complexType<double, double>::T abc);

你也可以这样做:

template void XYZ<double>(double);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多