模板类:ctor against function - >新的C ++标准 [英] template class: ctor against function -> new C++ standard

查看:107
本文介绍了模板类:ctor against function - >新的C ++标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:

在此问题中:

http://stackoverflow.com/questions/2779155/template-point2-double-point3-double

Dennis和Michael注意到不合理的愚蠢实现的构造函数。

是对的,我没有在这一刻考虑这一点。
但是我发现一个构造函数不像这样的模板类有很多帮助,而是一个函数在这里更方便和安全。

Hi
in this question:
http://stackoverflow.com/questions/2779155/template-point2-double-point3-double
Dennis and Michael noticed the unreasonable foolishly implemented constructor.
They were right, I didn't consider this at that moment. But I found out that a constructor does not help very much for a template class like this one, instead a function is here much more convenient and safe

namespace point {

template < unsigned int dims, typename T >
struct Point {

    T X[ dims ];

    std::string str() {
        std::stringstream s;
        s << "{";
        for ( int i = 0; i < dims; ++i ) {
            s << " X" << i << ": " << X[ i ] << (( i < dims -1 )? " |": " ");
        }
        s  << "}";
        return s.str();
    }

    Point<dims, int> toint() {
        Point<dims, int> ret;
        std::copy( X, X+dims, ret.X );
        return ret;
    }
};

template < typename T >
Point< 2, T > Create( T X0, T X1 ) {
    Point< 2, T > ret;
    ret.X[ 0 ] = X0; ret.X[ 1 ] = X1;
    return ret;
}
template < typename T >
Point< 3, T > Create( T X0, T X1, T X2 ) {
    Point< 3, T > ret;
    ret.X[ 0 ] = X0; ret.X[ 1 ] = X1; ret.X[ 2 ] = X2;
    return ret;
}
template < typename T >
Point< 4, T > Create( T X0, T X1, T X2, T X3 ) {
    Point< 4, T > ret;
    ret.X[ 0 ] = X0; ret.X[ 1 ] = X1; ret.X[ 2 ] = X2; ret.X[ 3 ] = X3;
    return ret;
}
};
int main( void ) {
    using namespace point;
    Point< 2, double > p2d = point::Create( 12.3, 34.5 );
    Point< 3, double > p3d = point::Create( 12.3, 34.5, 56.7 );
    Point< 4, double > p4d = point::Create( 12.3, 34.5, 56.7, 78.9 );
    //Point< 3, double > p1d = point::Create( 12.3, 34.5 ); //no suitable user defined conversion exists

    //Point< 3, int > p1i = p4d.toint(); //no suitable user defined conversion exists
    Point< 2, int > p2i = p2d.toint();
    Point< 3, int > p3i = p3d.toint();
    Point< 4, int > p4i = p4d.toint();

    std::cout << p2d.str() << std::endl;
    std::cout << p3d.str() << std::endl;
    std::cout << p4d.str() << std::endl;
    std::cout << p2i.str() << std::endl;
    std::cout << p3i.str() << std::endl;
    std::cout << p4i.str() << std::endl;

    char c;
    std::cin >> c;
}  

具有新的C ++标准关于此方面的任何新的改进,语言特性或简化的模板类的ctor?

你对命名空间,stuct和创建函数的组合的实现有什么看法?

很多感谢提前

糟糕

has the new C++ standard any new improvements, language features or simplifications regarding this aspect of ctor of a template class?
what do you think about the implementation of the combination of namespace, stuct and Create function?
many thanks in advance
Oops

推荐答案

是的,正如迈克尔在他对你上一个问题的回答中指出的,在C ++ 0x中,使用初始化器列表将任意数量的参数传递给您的ctor。在你的情况下,代码看起来像:

Yes, as Michael pointed out in his answer to your previous question, in C++0x you'll be able to use an initializer list to pass an arbitrary number of arguments to your ctor. In your case, the code would look something like:

template <int dims, class T>
class point { 
    T X[dims];
public:
    point(std::initializer_list<T> const &init) { 
        std::copy(init.begin(), init.begin()+dims, X);
    }
};

您可以使用以下内容创建点对象:

You could create a point object with this something like:

point<3, double> x{0.0, 0.0, 0.0};

个人而言,我不太确定我喜欢基本设计。特别是,我宁愿看到 X 变成 std :: vector ,并确定尺寸的数量从传递的参数列表中,而不是将其作为模板参数:

Personally, I'm not sure I like the basic design very well though. In particular, I'd rather see X turned into an std::vector, and determine the number of dimensions strictly from the parameter list that was passed instead of having it as a template argument:

template <class T>
class point { 
    std::vector<T> X;
public:
    point(std::initializer_list<T> init) {
        std::copy(init.begin(), init.end(), std::back_inserter(X));
    }
};

这有一些权衡,但是具有不同数量的维度的点仍然是相同的类型。例如,它基本上断言,将2D点分配给3D点是合理的,反之亦然。

This does have some trade-offs though -- points with a different number of dimensions are still the same type. For example, it basically asserts that it's reasonable to assign a 2D point to a 3D point, or vice versa.

这篇关于模板类:ctor against function - &gt;新的C ++标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆