我应该使用C ++ 0x功能现在吗? [英] Should I use C++0x Features Now?

查看:259
本文介绍了我应该使用C ++ 0x功能现在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS 2010的正式发布中,我是否可以开始使用我的新代码中部分实现的C ++ 0x功能集?

With the official release of VS 2010, is it safe for me to start using the partially-implemented C++0x feature set in my new code?

现在我感兴趣的功能都是由VC ++ 2010和最新版本的GCC实现的。

The features that are of interest to me right now are both implemented by VC++ 2010 and recent versions of GCC. These are the only two that I have to support.

在第一句中提到的安全方面:我可以开始使用这些特性(例如,lambda函数),仍然保证我的代码将在10年编译器上正确发布后正确符合C ++ 0x编译。

In terms of the "safety" mentioned in the first sentence: can I start using these features (e.g., lambda functions) and still be guaranteed that my code will compile in 10 years on a compiler that properly conforms to C++0x when it is officially released?

我想我问,如果有任何机会,VC ++ 2010或GCC将最终像VC ++ 6;它是在语言正式标准化之前发布的,因此允许编译严重错误的代码。

I guess I'm asking if there is any chance that VC++ 2010 or GCC will end up like VC++ 6; it was released before the language was officially standardized and consequently allowed grossly ill-formed code to compile.

毕竟,微软确实说10是新的6。 ;)

After all, Microsoft does say that "10 is the new 6". ;)

推荐答案

我已经发现有几个项目没有写入标准。例如,这将不起作用:

There are several items I've already discovered that are not written to the standard. For instance, this would not work:



struct test {
  int operator()(int);
};

std::cout << typeid( std::result_of<test(int)>::type ).name() << std::endl;

根据wikipedia网站上的C ++ 0x应该。显然VS2010使用的result_of的TR1定义,这是不同于C ++ 0x将有(基于decltype)。

According to the wikipedia site on C++0x it should. Apparently VS2010 uses the TR1 definition of result_of, which is different from what C++0x will have (based on decltype).

此外,这不工作: p>

Also, this does not work:



std::bind<int>([](int i)->int {return i; });

它失败,因为调用std :: result_of(好,它的实现),失败,因为lambda类型没有result_of typedef。这当然是为什么你提供返回类型的绑定调用,但显然它忽略它的某些原因,并继续自己搜索。 bind的boost版本按预期工作。因此,我们将继续在我们的项目中使用boost的boost版本。

It fails because calling std::result_of (well, the implementation of it), fails because the lambda type has no result_of typedef. This is of course why you supply the return type to the bind call but apparently it ignores it for some reason and continues to search on its own. The boost version of bind works as expected. For this reason we're continuing to use the boost version of bind in our project.

此外,如果您注意到http://blogs.msdn .com / vcblog / archive / 2010/04/06 / c-0x-core-language-features-in-vc10-the-table.aspx?CommentPosted = true#commentmessage 有一些更改尚未实施由VS2010将影响lambda表达式。我还没有能够打破他们,但是我没有使用嵌套的lambdas,可能永远不会。

Also, if you'll note on http://blogs.msdn.com/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx?CommentPosted=true#commentmessage that there's some changes yet to be implemented by VS2010 that will effect lambda expressions. I haven't been able to break them but then I haven't used nested lambdas and probably never will.

您还应该记住boost :: shared_ptr和std :: shared_ptr不兼容。不奇怪,但你必须知道这一点,如果你打算使用一个或另一个...我建议不是两个,我们只是坚持用boost。

You should also keep in mind that boost::shared_ptr and std::shared_ptr are incompatible. Not surprising, but you must know this if you intend to use one or the other...I'd recommend not both and we're just going to stick with boost.

VS2010中也没有声明。很容易做到:

There's also no declval in VS2010. Easy enough to make though:


template < typename T > T&& declval();

使用示例:


template < typename T >
struct point
{
  T x,y;
};

template < typename T1, typename T2 >
point<decltype(declval<T1>() + declval<T2>())> operator + (point<T1> const& lh, point<T2> const& rh)
{
 ...
}

在上面链接的页面中,我还会注意到,我已经与开发团队的成员(或PR部分或其他)讨论过decltype中有一个错误。不仅仅是我提到的一个,所以我会显示两个:

You'll also note in the page I linked above that I've already discussed with members of the dev team (or the PR part or whatever) that there's a bug in decltype. There's more than just the one I mention so I'll show both:


template < typename T1, typename T2 >
auto operator + (point<T1> const& lh, point<T2> const& rh)
  -> point<decltype(lh.x + rh.x)>
{
...
}
point<int> x; point<double> y;
point<double> pt = x + y; // fails, operator + returned point<const double>

void f();
auto ptr = &f;

std::cout << typeid( decltype(*ptr) ).name() << std::endl;
std::cout << typeid( decltype(*&f) ).name() << std::endl; // should output the same thing...outputs void (*)()

根据一些关于decltype和result_of的电子邮件交换,这应该工作:

Also...according to some email exchanges about decltype and result_of, this should work:



std::result_of< decltype(f)() >::type x = f();

我的std :: result_of的自制酿造版本使用decltype这将工作if decltype(f)()表达式正确工作。它不是。给出返回函数的函数的一些错误。你必须使用decltype(& f)()使表达式工作。

With my home brewed version of std::result_of that uses decltype this would work if the decltype(f)() expression worked correctly. It does not. Gives some error about function returning a function. You have to use "decltype(&f)()" to make the expression work.

所以,我们使用它。有一些错误和胡扯。好处胜过等待IMHO。不要指望你的代码是标准的,当标准出来,而未来的MS编译器可能会破坏它。

So, sure...we're using it. There's some bugs and crap though. The benefits outweigh waiting IMHO. Don't expect your code to be standard when the standard comes out though and future MS compilers may break it.

这篇关于我应该使用C ++ 0x功能现在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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