【问题标题】:How can I get a specialized template to use the unspecialized version of a member function?如何获得专门的模板来使用成员函数的非专门版本?
【发布时间】:2008-12-07 01:43:55
【问题描述】:

考虑以下代码:

template <int dim>
struct vec
{
    vec normalize();
};


template <>
struct vec<3>
{
    vec cross_product(const vec& second);
    vec normalize();
};

template <int dim>
vec<dim> vec<dim>::normalize()
{
    // code to normalize vector here
    return *this;
}

int main()
{
    vec<3> direction;
    direction.normalize();
}

编译此代码会产生以下错误:

1>main.obj : error LNK2019: unresolved external symbol "public: struct vec __thiscall vec::normalize(void)" (?normalize@?$vec@$02@@QAE?AU1@ XZ) 在函数_main中引用

【问题讨论】:

  • l0)p7niYnhnb?~Hh)6?R.G!HBHUbJp

标签: c++ templates specialization


【解决方案1】:

你不能:)你想要的是专门化成员函数:

template <int dim>
struct vec
{
    // leave the function undefined for everything except dim==3
    vec cross_product(const vec& second);
    vec normalize();
};

template<>
vec<3> vec<3>::cross_product(const vec& second) {
    // ...
}

template <int dim>
vec<dim> vec<dim>::normalize()
{
    // code to normalize vector here
    return *this;
}

另一个稍微复杂一点的解决方案是使用boost::enable_if:

template <int dim>
struct vec
{
    // function can't be called for dim != 3. Error at compile-time
    template<int dim1>
    typename boost::enable_if_c< dim == dim1 && dim1 == 3, vec<dim1> >::type 
    cross_product(const vec<dim1>& second) {
        // ...
    }
    vec normalize();

    // delegate to the template version
    void without_params() {
        // delegate
        this->without_params<dim>();
    }

private:
    // function can't be called for dim != 3. Error at compile-time
    template<int dim1>
    typename boost::enable_if_c< dim == dim1 && dim1 == 3 >::type 
    without_params() {
        // ...
    }   
};

template <int dim>
vec<dim> vec<dim>::normalize()
{
    // code to normalize vector here
    return *this;
}

如果为任何 dim != 3 调用 cross_product,这将导致编译时错误。请注意,“技巧”仅适用于带参数的函数,因为只有这样才能自动推导出模板参数。对于没有参数的情况,我在上面提供了一个函数without_parameters :)。

【讨论】:

  • 我真的很喜欢这个解决方案,没有冗余代码概念上相同的功能。我现在意识到我错误地使用了类专业化。
  • 我昨天用模板参数名称搞砸了一点。现在它还包含一个不带参数的函数版本。享受:)
  • 不。如果参数的替换违反 c++ 规则或访问不存在的类型,它只是一种将模板从重载解析中排除的机制。留意“SFINAE”。
  • 您可以从一个模板派生模板,该模板本身专门为不同的参数提供不同的成员。但是如果你发现你需要专门化成员变量,那么考虑是否最好去专门化整个模板本身。
  • da_code_monkey,如果你对 enable_if_c 的实现感兴趣,你可以在这里得到它的定义:stackoverflow.com/questions/257288/…(enable_if 的作用类似于 boost::enable_if_c)
【解决方案2】:

您没有提供 vec::normalize 的定义,因此链接器显然无法链接到它。

模板专业化的重点是您可以提供每个方法的专业化版本。除非在这种情况下您实际上并没有这样做。

【讨论】:

    【解决方案3】:

    据我所知,你不能称之为“通用”版本。

    或者,您可以将类之外的通用实现定义为函数:

    template <int dim>
    struct vec
    {
    };
    
    namespace impl {
        template <int dim>
        vec<dim> normalize(const vec<dim>& v)
        {
            // code to normalize vector here
            return v;
        }
    }
    
    template <>
    struct vec<3>
    {
        vec cross_product(const vec& second);
        vec normalize() { return impl::normalize(*this); }
    };
    
    
    int main()
    {
        vec<3> direction;
        direction.normalize();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多