1

我在 C++ 中定义了一个类,它包含一个类型的标量数组T,我想为其定义诸如 sin、cos 等运算符。为了定义sin应用于此类的对象的含义,我需要知道sin应用于的含义单一的标量类型T。这意味着我需要在T类中使用适当的数学库(对应于标量类型)。这是现在的代码:

template<class T>
class MyType<T>
{
    private:
        std::vector<T> list;

    // ...

        template<class U> friend const UTP<U> sin(const UTP<U>& a);
        template<class U> friend const UTP<U> cos(const UTP<U>& a);
        template<class U> friend const UTP<U> tan(const UTP<U>& a);

    //...
};

template<class T> const UTP<T> sin(const UTP<T>& a)
{
   // use the sin(..) appropriate for type T here 
   // if T were double I want to use double std::sin(double)
   // if T were BigNum I want to use BigNum somelib::bigtype::sin(BigNum)
}

目前,我的代码公开了适当的数学库(使用命名空间 std;),然后::sin(a)在我的类的 sin 函数中使用MyType。虽然这有效,但它似乎是一个重大的黑客攻击。

我看到 C++ 特征可用于存储实例特定信息(例如使用哪组数学函数 when Tis double, when TisBigNum等)

我想做这样的事情:(我知道这不会编译,但我希望这能传达我想要做的事情)

template<T>
struct MyType_traits {
};

template<>
struct MyType_traits<double> {
    namespace math = std;
};

template<>
struct MyType_traits<BigNum> {
    namespace math = somelib::bigtype;
};

然后将我的 MyType 类重新定义为:

template<T, traits = MyType_traits<T> >
class MyType
{
// ...
}

然后traits::math::sin在我的朋友功能中使用。有没有一种方法可以获得T包含数学函数的正确命名空间(由 参数化)?

4

3 回答 3

5

依赖于参数的查找还不够好吗?

#include <cmath>
#include <iostream>

namespace xxx {
class X
{
};

X sin(X) { return X(); }
} //xxx

std::ostream& operator<< (std::ostream& os, xxx::X)
{
    return os << "X";
}

template <class T>
void use_sin(T t)
{
    using std::sin; //primitive types are not in a namespace,
                    //and with some implementation sin(double) etc might not be available
                    //in global namespace
    std::cout << sin(t) << '\n';
}

int main()
{
    use_sin(1.0);
    use_sin(xxx::X());
}

这适用于 X,因为sin(X)它定义在与 X 相同的命名空间中。如果您希望不是这样,这可能无济于事......

于 2010-08-07T19:41:13.167 回答
0

我之所以包含这个答案,是因为我终于设法得到了我想要的东西(在 irc.freenode.net 上##c++ 上非常好的人的帮助下)。此方法使 ADL 以及一组静态位置 (xxx::math) 都可以查找数学函数的定义。

这样,如果类 Test 的类型参数 T 是这样的:

  1. 如果 T 将数学函数定义为成员,那么我们可以使用 ADL 而不会触及(添加到)命名空间 xxx::math。
  2. 如果 T 没有将数学函数定义为成员,而是使用来自特定命名空间的函数,那么我们可以添加到命名空间 xxx::math,如下例所示。

图书馆看起来像这样:

#include <vector>
#include <cmath>

namespace xxx {

// include the usual math
namespace math {
    using std::asin;
}

template <class T>
class Test
{
    std::vector<T> array;

    public:
    Test(const typename std::vector<T>::size_type length)
    {
        assert(length >= 1);
        array.assign(length, T(0.0));
    }
    friend std::ostream& operator<<(std::ostream& out, const Test<T>& a)
    {
        out << "(";
        std::copy(a.array.begin(), a.array.end(), std::ostream_iterator<T>(out, ", "));
        out << "\b\b)";
        return out;
    }

    template<class U> friend const Test<U> asin(const Test<U>& a);
};

template<class U> const Test<U> asin(const Test<U>& a)
{
    using math::asin;

    Test<U> ret(a.array.size());
    for (typename std::vector<U>::size_type i = 0; i < a.array.size(); ++i) 
        ret.array[i] = asin(a.array[i]);

    // note how we use have a using math::asin; and then a call to asin(..) here,
    // instead of a math::asin(..). This allows for ADL.

    return ret;
}

} // xxx

客户端看起来像这样:

#include <iostream>
#include <boost/math/complex.hpp>

// client, with some foresight, includes complex math
namespace xxx { namespace math {
    using boost::math::asin;
} }

#include "test.h"

// demo
int main(int argc, char **argv)
{
    using std::cout; using std::endl;

    xxx::Test<double> atest(3);
    cout << "atest: " <<  atest << endl;
    cout << "asin(atest): " <<  asin(atest) << endl;
    cout << endl;

    xxx::Test<std::complex<double> > btest(3);
    cout << "btest: " <<  btest << endl;
    cout << "asin(btest): " <<  asin(btest) << endl;
    cout << endl;

    return 0;
}
于 2010-08-08T12:26:01.793 回答
0

这不是您要寻找的具体答案,但使用模板专业化不是更简单的选择吗?

如...

template <typename T> T sin(T& t)
{
    // does nothing
}

template <> float sin(float& t)
{
    ...
}

template <> double sin(double& t)
{
    ...
}

等等?

于 2010-08-07T19:22:53.920 回答