命名构造函数习惯用法和模板?
有没有办法使用 命名构造函数惯用语 “漂亮”时尚的模板?
例如:
#include <vector>
using namespace std;
template< typename T >
class Foo
{
public:
static Foo Copy(const T& arg)
{
Foo ret;
ret.t_copy = arg;
return ret;
}
static Foo CopyClear(const T& arg)
{
Foo ret;
ret.t_copy = arg;
ret.t_copy.clear();
return ret;
}
private:
T t_copy;
};
int main( int argc, char** argv )
{
vector<double> vec;
vec.push_back(1);
// #1: won't compile
Foo< vector<double> > a_foo = Foo::CopyClear( vec );
// #2: ugly, but works
Foo< vector<double> > a_foo = Foo< vector<double> >::CopyClear( vec );
return 0;
}
我想以某种方式使用 #1
的语法。 #2
有效,但让我的 DRY 感觉错误。
编辑:Foo
的新的、更“现实”的版本。
EDIT2:恐怕我没有 C++0x/C++1x :(
Is there a way to use the Named Constructor Idiom with templates in a "pretty" fashion?
For instance:
#include <vector>
using namespace std;
template< typename T >
class Foo
{
public:
static Foo Copy(const T& arg)
{
Foo ret;
ret.t_copy = arg;
return ret;
}
static Foo CopyClear(const T& arg)
{
Foo ret;
ret.t_copy = arg;
ret.t_copy.clear();
return ret;
}
private:
T t_copy;
};
int main( int argc, char** argv )
{
vector<double> vec;
vec.push_back(1);
// #1: won't compile
Foo< vector<double> > a_foo = Foo::CopyClear( vec );
// #2: ugly, but works
Foo< vector<double> > a_foo = Foo< vector<double> >::CopyClear( vec );
return 0;
}
I'd like to use the syntax of #1
somehow. #2
works but rubs my DRY sense the wrong way.
EDIT: New, more "realistic" version of Foo
.
EDIT2: No C++0x/C++1x for me I'm afraid :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新的答案
如果我正确理解你的意图,这将达到目的:
然后编译:
Updated answer
If I understand your intent correctly, this will do the trick:
This then compiles:
我不认为存在 DRY 问题,将其视为语言限制。
如果您有一个没有模板的 Foo 类,但您想从静态方法创建一个新对象,则必须执行以下操作:
当然 Foo 重复了两次。
因此,由于这里完整的类名称是
Foo<;矢量<双> >
,从Foo< 获取静态方法是符合逻辑的。矢量<双> >::HalfSize()
,因为这是 C++ 的方式。I don't think there is a DRY problem, think of it as a language restriction.
If you have a Class Foo without template but you want to create a new object from a static method, you'd have to do something like:
and there's of course the Foo repeated twice.
So, since here the full Class name is
Foo< vector<double> >
, it's logical to get the static method fromFoo< vector<double> >::HalfSize()
, since that's the C++ way.除了 @Jon 的答案之外,如果您需要类本身作为模板,请参阅
std::make_pair
及其与std::pair
的关系。In addition to @Jon's answer, see
std::make_pair
and its relationship tostd::pair
, if you need the class itself to be a template.这在技术上是可以的,并且可能是对您的最终问题的最简单答案:
技术上可以,因为
CopyClear
是一个静态
会员功能。并且不存在技术问题,例如您可以使用
typedef
代替。或者只是将这些静态成员函数作为函数模板放在命名空间范围内。或者在一些辅助类中,正如有人已经建议的那样。但即使没有技术问题,设计也不太理想;坦率地说(抱歉),这比毫无意义更糟糕。
例如,在
CopyClear
中,为什么要复制向量然后丢弃复制结果?您只需要创建一个您的代码知道的类型的空向量。例如,为什么要引入副作用机制?
副作用是要避免和消除的,而不是引入的。
干杯&呵呵,
This is technically OK and is possibly the simplest answer to your end question:
It's technically OK because
CopyClear
is astatic
member function.And there is no technical problem, e.g. you could use a
typedef
instead. Or just put thosestatic
member functions at namespace scope, as function templates. Or in some helper class, as someone has already suggested.But even though there is no technical problem, the design is less than ideal; it is, to put it bluntly (sorry), a bit worse than meaningless.
For example, in
CopyClear
, why are you copying a vector and then discarding the copy result? You only need to create an empty vector, of the type your code knows.And for example, why are you introducing side-effect machinery?
Side-effects are to be avoided and removed, not introduced.
Cheers & hth.,
如果您可以使用 C++0x 功能,
auto
关键字将会有所帮助。Size()
和HalfSize()
需要是静态方法是否有原因?如果您提供改变sz
的方法,您可以这样做:然后#1 更容易实现。
If you can use C++0x features, the
auto
keyword would help. Is there a reason thatSize()
andHalfSize()
need to be static methods? If you provide methods to mutatesz
you can do this:and then #1 is a little more attainable.
C++1x 来救援:
是的,两个结束
>>
被解析为> C++1x 中的 >
。您附近的编译器可能已经可用。
C++1x to the rescue:
And, yes, the two closing
>>
are parsed as> >
in C++1x.Probably already available with a compiler near you.