成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

來自VC++小組的VS2010 Beta 1常見問題報(bào)告

原創(chuàng)
開發(fā) 后端
Visual Studio 2010 Beta 1中的VC++對(duì)C++0x提供了很多支持,很多標(biāo)準(zhǔn)也與C++0x接軌。這樣做的好處是很多的,但同時(shí)也會(huì)導(dǎo)致以前一些不符合C++0x規(guī)范的代碼無法像原來那樣工作。以下是VC++小組的相關(guān)問題報(bào)告。

【51CTO獨(dú)家報(bào)道】Visual Studio 2010 Beta 1已經(jīng)發(fā)布了一周有余。這期間,Visual C++小組一直在測(cè)試VC++在VS 2010 Beta 1下運(yùn)行的情況(或者叫做VC10 Beta 1)。以下是VC++小組的類庫(kù)開發(fā)者Stephan T. Lavavej帶來的幾個(gè)可能導(dǎo)致原有代碼崩潰的問題報(bào)告。

Visual Studio 2010 Beta 1 is now available for download.  I've recently blogged about how Visual C++ in VS 2010 Beta 1, which I refer to as VC10 Beta 1, contains compiler support for five C++0x core language features: lambdas, auto, static_assert, rvalue references, and decltype.  It also contains a substantially rewritten implementation of the C++ Standard Library, supporting many C++0x standard library features.  In the near future, I'll blog about them in Part 4 and beyond of "C++0x Features in VC10", but today I'm going to talk about the STL changes that have the potential to break existing code, which you'll probably want to know about before playing with the C++0x goodies.

(VS2010 Beta 1現(xiàn)在可以下載了。我最近寫了些博文,關(guān)于Visual C++在VS 2010 Beta 1版中對(duì)5個(gè)C++0x核心語(yǔ)言:lambda,auto,static_assert,rvalue references以及decltype的支持情況。我之后會(huì)用VC10 Beta 1這個(gè)詞來說明。……今天我將討論一下一些有可能會(huì)導(dǎo)致現(xiàn)有代碼崩潰的幾個(gè)STL變動(dòng)。在你使用C++0x開發(fā)之前可能會(huì)想要了解一下。)

問題1:error C3861: 'back_inserter': identifier not found

在VC9 SP1下這是沒問題的:

C:\Temp﹥type back_inserter.cpp
#include ﹤algorithm﹥
#include ﹤iostream﹥
#include ﹤ostream﹥
#include ﹤vector﹥
using namespace std;
 
int square(const int n) {
    return n * n;
}
 
int main() {
    vector﹤int﹥ v;
    v.push_back(11);
    v.push_back(22);
    v.push_back(33);
 
    vector﹤int﹥ dest;
 
    transform(v.begin(), v.end(), back_inserter(dest), square);
 
    for (vector﹤int﹥::const_iterator i = dest.begin(); i != dest.end(); ++i) {
        cout ﹤﹤ *i ﹤﹤ endl;
    }
}
 
C:\Temp﹥cl /EHsc /nologo /W4 back_inserter.cpp
back_inserter.cpp
 
C:\Temp﹥back_inserter
121
484
1089

VC10 Beta 1下就有問題了:

C:\Temp﹥cl /EHsc /nologo /W4 back_inserter.cpp
back_inserter.cpp
back_inserter.cpp(19) : error C3861: 'back_inserter': identifier not found

出了什么問題?

解決方法:#include ﹤iterator﹥

The problem was that back_inserter() was used without including ﹤iterator﹥.  The C++ Standard Library headers include one another in unspecified ways.  "Unspecified" means that the Standard allows but doesn't require any header X to include any header Y.  Furthermore, implementations (like Visual C++) aren't required to document what they do, and are allowed to change what they do from version to version (or according to the phase of the moon, or anything else).  That's what happened here.  In VC9 SP1, including ﹤algorithm﹥ dragged in ﹤iterator﹥.  In VC10 Beta 1, ﹤algorithm﹥ doesn't drag in ﹤iterator﹥.

(問題在于,back_inserter()在沒有include ﹤iterator﹥的情況下被實(shí)用。C++標(biāo)準(zhǔn)庫(kù)的headers會(huì)通過某種未定義的方式將其他headers包括進(jìn)來?!璙C9 SP1下,include﹤algorithm﹥順帶的就包括了﹤iterator﹥,但在VC10 Beta 1下需要單獨(dú)寫明。)

When you use a C++ Standard Library component, you should be careful to include its header (i.e. the header that the Standard says it's supposed to live in).  This makes your code portable and immune to implementation changes like this one.

There are probably more places where headers have stopped dragging in other headers, but ﹤iterator﹥ is overwhelmingly the most popular header that people have forgotten to include.

注意:Range Insertion和Range Construction

By the way, when seq is a vector, deque, or list, instead of writing this:

(當(dāng)seq是一個(gè)vector,deque或list的時(shí)候,不要這樣寫:)

copy(first, last, back_inserter(seq)); // 這是不好的!

而應(yīng)該這樣寫:

seq.insert(seq.end(), first, last); // Range Insertion - 好的寫法

如果只是在創(chuàng)建seq,只要這樣寫就可以了:

vector﹤T﹥ seq(first, last); // Range Construction - 好的寫法

They're not only slightly less typing, they're also significantly more efficient.  copy()-to-back_inserter() calls push_back() repeatedly, which can trigger multiple vector reallocations.  Given forward or better iterators, range insertion and range construction can just count how many elements you've got, and allocate enough space for all of them all at once.  This is also more efficient for deque, and you may as well do it for list too.
 

#p#
 
問題2: error C2664: 'std::vector﹤_Ty﹥::_Inside' : cannot convert parameter 1 from 'IUnknown **' to 'const ATL::CComPtr﹤T﹥ *'

在VC9 SP1下這是沒問題的:

C:\Temp﹥type vector_ccomptr.cpp
#include ﹤atlcomcli.h﹥
#include ﹤stddef.h﹥
#include ﹤iostream﹥
#include ﹤ostream﹥
#include ﹤vector﹥
using namespace std;
 
int main() {
    vector﹤CComPtr﹤IUnknown﹥﹥ v;
 
    v.push_back(NULL);
}
 
C:\Temp﹥cl /EHsc /nologo /W4 vector_ccomptr.cpp
vector_ccomptr.cpp
 
C:\Temp﹥vector_ccomptr
 
C:\Temp﹥

VC10 Beta 1下就有問題了:
 

C:\Temp﹥cl /EHsc /nologo /W4 vector_ccomptr.cpp
vector_ccomptr.cpp
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\vector(623) : error C2664: 'std::vector﹤_Ty﹥::_Inside' : cannot convert parameter 1 from 'IUnknown **' to 'const ATL::CComPtr﹤T﹥ *'
        with
        [
            _Ty=ATL::CComPtr﹤IUnknown﹥
        ]
        and
        [
            T=IUnknown
        ]
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\vector(622) : while compiling class template member function 'void std::vector﹤_Ty﹥::push_back(_Ty &&)'
        with
        [
            _Ty=ATL::CComPtr﹤IUnknown﹥
        ]
        vector_ccomptr.cpp(9) : see reference to class template instantiation 'std::vector﹤_Ty﹥' being compiled
        with
        [
            _Ty=ATL::CComPtr﹤IUnknown﹥
        ]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\vector(625) : error C2040: '-' : 'IUnknown **' differs in levels of indirection from 'ATL::CComPtr﹤T﹥ *'
        with
        [
            T=IUnknown
        ]

出了什么問題?

解決方法:使用CAdapt

The Standard containers prohibit their elements from overloading the address-of operator.  CComPtr overloads the address-of operator.  Therefore, vector﹤CComPtr﹤T﹥﹥ is forbidden (it triggers undefined behavior).  It happened to work in VC9 SP1, but it doesn't in VC10 Beta 1.  That's because vector now uses the address-of operator in push_back(), among other places.

The solution is to use ﹤atlcomcli.h﹥'s CAdapt, whose only purpose in life is to wrap address-of-overloading types for consumption by Standard containers.  vector﹤CAdapt﹤CComPtr﹤T﹥﹥﹥ will compile just fine.  In VC10 Beta 1, I added operator-﹥() to CAdapt, allowing v[i]-﹥Something() to compile unchanged.  However, typically you'll have to make a few other changes when adding CAdapt to your program.  operator.() can't be overloaded, so if you're calling CComPtr's member functions like Release(), you'll need to go through CAdapt's public data member m_T .  For example, v[i].Release() needs to be transformed into v[i].m_T.Release() .  Also, if you're relying on implicit conversions, CAdapt adds an extra layer, which will interfere with them.  Therefore, you may need to explicitly convert things when pushing them back into the vector.

(標(biāo)準(zhǔn)容器禁止其元素將運(yùn)算符的address-of過載。CComPtr將運(yùn)算符的address-of過載,導(dǎo)致 vector﹤CComPtr﹤T﹥﹥被禁止。這是因?yàn)?,在VC9 SP1中,vector并在push_back()中使用不使用運(yùn)算符的address-of,但在VC10 Beta 1中是使用的。

解決方法就是使用﹤atlcomcli.h﹥的CAdapt?!?BR> 
 #p#

問題3: error C2662: 'NamedNumber::change_name' : cannot convert 'this' pointer from 'const NamedNumber' to 'NamedNumber &'

在VC9 SP1下這是沒問題的:
 

C:\Temp﹥type std_set.cpp
#include ﹤iostream﹥
#include ﹤ostream﹥
#include ﹤set﹥
#include ﹤string﹥
using namespace std;
 
class NamedNumber {
public:
    NamedNumber(const string& s, const int n)
        : m_name(s), m_num(n) { }
 
    bool operator﹤(const NamedNumber& other) const {
        return m_num ﹤ other.m_num;
    }
 
    string name() const {
        return m_name;
    }
 
    int num() const {
        return m_num;
    }
 
    void change_name(const string& s) {
        m_name = s;
    }
 
private:
    string m_name;
    int m_num;
};
 
void print(const set﹤NamedNumber﹥& s) {
    for (set﹤NamedNumber﹥::const_iterator i = s.begin(); i != s.end(); ++i) {
        cout ﹤﹤ i-﹥name() ﹤﹤ ", " ﹤﹤ i-﹥num() ﹤﹤ endl;
    }
}
 
int main() {
    set﹤NamedNumber﹥ s;
 
    s.insert(NamedNumber("Hardy", 1729));
    s.insert(NamedNumber("Fermat", 65537));
    s.insert(NamedNumber("Sophie Germain", 89));
 
    print(s);
 
    cout ﹤﹤ "--" ﹤﹤ endl;
 
    set﹤NamedNumber﹥::iterator i = s.find(NamedNumber("Whatever", 1729));
 
    if (i == s.end()) {
        cout ﹤﹤ "OH NO" ﹤﹤ endl;
    } else {
        i-﹥change_name("Ramanujan");
    }
 
    print(s);
}
 
C:\Temp﹥cl /EHsc /nologo /W4 std_set.cpp
std_set.cpp
 
C:\Temp﹥std_set
Sophie Germain, 89
Hardy, 1729
Fermat, 65537
--
Sophie Germain, 89
Ramanujan, 1729
Fermat, 65537

VC10 Beta 1下就有問題了:

C:\Temp﹥cl /EHsc /nologo /W4 std_set.cpp
std_set.cpp
std_set.cpp(55) : error C2662: 'NamedNumber::change_name' : cannot convert 'this' pointer from 'const NamedNumber' to 'NamedNumber &'
        Conversion loses qualifiers

出了什么問題?

解決方法:Respect set Immutability (尊重set的不變性) 

The problem is modifying set/multiset elements.

(問題出在set/multiset元素的修改上。)

In C++98/03, you could get away with modifying set/multiset elements as long as you didn't change their ordering.  (Actually changing their ordering is definitely crashtrocity, breaking the data structure's invariants.)

C++0x rightly decided that this was really dangerous and wrong.  Instead, it flat-out says that "Keys in an associative container are immutable" (N2857 23.2.4/5) and "For [set and multiset], both iterator and const_iterator are constant iterators" (/6).

(C++0x決定,修改set/multiset元素是危險(xiǎn)的和不正確的?!?/P>

VC10 Beta 1 enforces the C++0x rules.

(VC10 Beta 1遵循C++0x的規(guī)則。) 

There are many alternatives to modifying set/multiset elements.

(以下是修改set/multiset元素的一些代替的方法。)

◆You can use map/multimap, separating the immutable key and modifiable value parts.

◆You can copy, modify, erase(), and re-insert() elements.  (Keep exception safety and iterator invalidation in mind.)

◆ You can use set/multiset﹤shared_ptr﹤T﹥, comparator﹥, being careful to preserve the ordering and proving once again that anything can be solved with an extra layer of indirection.

◆You can use mutable members (weird) or const_cast (evil), being careful to preserve the ordering.  I strongly recommend against this.

I should probably mention, before someone else discovers it, that in VC10 Beta 1 we've got a macro called _HAS_IMMUTABLE_SETS .  Defining it to 0 project-wide will prevent this C++0x rule from being enforced.  However, I should also mention that _HAS_IMMUTABLE_SETS is going to be removed after Beta 1.  You can use it as a temporary workaround, but not as a permanent one.
 
#p# 

問題4:Specializing stdext::hash_compare

If you've used the non-Standard ﹤hash_set﹥ or ﹤hash_map﹥ and specialized stdext::hash_compare for your own types, this won't work anymore, because we've moved it to namespace std.  ﹤hash_set﹥ and ﹤hash_map﹥ are still non-Standard, but putting them in namespace stdext wasn't accomplishing very much.

(如果你為你的type使用非標(biāo)準(zhǔn)話的 ﹤hash_set﹥或﹤hash_map﹥,以及特別的stdext::hash_compare,那你會(huì)發(fā)現(xiàn)這里沒法兒用了,因?yàn)樗鼈儽灰频搅嗣臻g下。 ……)
 
解決方法:使用 ﹤unordered_set﹥ 或﹤unordered_map﹥

TR1/C++0x ﹤unordered_set﹥ and ﹤unordered_map﹥ are powered by the same machinery as ﹤hash_set﹥ and ﹤hash_map﹥, but the unordered containers have a superior modern interface.  In particular, providing hash and equality functors is easier.

If you still want to use ﹤hash_set﹥ and ﹤hash_map﹥, you can specialize std::hash_compare, which is where it now lives.  Or you can provide your own traits class.

By the way, for those specializing TR1/C++0x components, you should be aware that they still live in std::tr1 and are dragged into std with using-declarations.  Eventually (after VC10) this will change.

This isn't an exhaustive list, but these are the most common issues that we've encountered.  Now that you know about them, your upgrading experience should be more pleasant.

(列表并不全面,但是這是我們所最常遇到的問題?,F(xiàn)在你也知道了,希望你的更新過程更加愉快。)

Stephan T. Lavavej

Visual C++ Libraries Developer

內(nèi)容來源:Visual C++ 小組博客

【編輯推薦】

  1. Visual Studio 2010的歷史調(diào)試功能
  2. 探秘Visual Studio 2010中的災(zāi)難恢復(fù)功能
  3. 在Visual Studio 2010中自定義開始頁(yè)
  4. 微軟發(fā)布Visual Studio 2010 Beta 1
  5. Visual Studio 2010重要新功能一覽
  6. Visual C# 2010新特性之dynamic類型
  7. 推薦Visual Studio 2010中F#的一些資源
責(zé)任編輯:yangsai 來源: 51CTO.com
相關(guān)推薦

2009-06-10 22:41:47

Visual StudVS2010ASP.NET MVC

2009-07-28 10:00:47

VS2010 beta

2009-05-21 14:42:09

.NET 4.0VS2010Visual Stud

2009-12-02 14:44:09

VS2010 Beta

2009-12-02 14:19:09

VS 2010產(chǎn)品

2009-12-11 15:13:15

VS 2010驅(qū)動(dòng)

2009-11-11 11:29:37

VS2010 Auto

2009-12-02 16:02:58

VS2010編輯器

2011-08-16 14:50:05

CMFCToolBarVS2010

2009-12-15 17:42:29

2009-12-11 14:16:11

VS2010 Ulti

2009-12-15 17:55:54

VS2010 Ulti

2009-12-04 10:02:32

VS XML注釋

2010-03-24 09:06:02

Visual Stui

2009-05-27 09:45:40

2009-12-02 14:05:17

VS2010程序

2009-12-18 10:24:28

VS 2010代碼

2009-05-20 13:03:40

Visual StudSilverlight微軟

2010-12-28 09:39:18

std::tr1 biVS2010

2009-11-04 09:59:20

Visual StudFlashNetBeans
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 欧洲精品在线观看 | 久草资源在线 | 国产精品永久久久久 | 亚洲va国产日韩欧美精品色婷婷 | 成人午夜激情 | 国产精品成人一区二区三区 | 一级欧美 | 国产精品一区二区久久久久 | 亚洲激情一区二区三区 | 中文字幕在线观看av | 欧美日韩亚洲视频 | 欧美亚洲国产一区 | 午夜精品一区二区三区免费视频 | 国产日韩精品视频 | 日韩一级一区 | 欧美性久久| 国产精品视频久久久 | 开操网| 免费在线黄| 亚洲日本一区二区三区四区 | 国产精品黄视频 | 日本不卡一区 | 成人在线视频网站 | 亚洲一区二区中文字幕在线观看 | 九九九国产 | 久久天堂 | 欧美日韩国产一区二区三区 | 一区二区国产在线观看 | 国产精品久久久久久影视 | 亚洲福利视频一区二区 | 精品久久久久久久久久久久 | 亚洲精品在线视频 | 蜜臀网 | 亚洲视频在线免费观看 | 天天曰夜夜操 | 香蕉二区| 日韩中文字幕第一页 | 国产成人综合在线 | 91在线一区二区三区 | av一区二区三区四区 | 久久久久一区 |