欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

static_assertenable_if模板編譯期檢查-創(chuàng)新互聯(lián)

conceptC++

http://www.generic-programming.org/faq/?category=conceptcxx

Checking Concept Without Concepts in C++

By Anthony Williams, September 22, 2010static_assertena
ble_if模板編譯期檢查

1 Comment

專注于為中小企業(yè)提供網(wǎng)站建設、成都網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)新田免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。Get the benefits of C++0x but without the work

Anthony Williams is author of the book C++ Concurrency in Action and of the just::thread C++0x thread library. He can be contacted at anthony.ajw@gmail.com


"Concepts" was set to be one of the major selling points of the new C++0x standard, until it was removed from the draft in July 2009. The "Concepts" feature promised better compile-time checking of templates, and the ability to overload functions based on whether or not their parameters supported specific templates.

In this article I look at some techniques for obtaining some of the benefits of concepts, with the facilities we already have in C++. Source code that illustrates these techniques is listed at the end of this article.

Basic Tools

The basic tools we need for concept checking and concept-based overloading are static_assertenable_if, and a particular property of template instantiation dubbed SFINAE (short for "Substitution Failure Is Not An Error").static_assert is useful for the checking, enable_if for the overloading. SFINAE is the mechanism that makesenable_if work, and can be used to write additional checks for testing with static_assert or enable_if.

static_assert is a new C++0x language feature, and is available in the latest versions of some compilers, such as Microsoft Visual Studio 2010 and g++ 4.3 or later. It allows you to specify a boolean constant expression and an error message -- if the constant expression evaluates to False at compile time then the compilation fails and the specified error message is output. For example, this simple program compiled with gcc -std=c++0x:

int main() { static_assert(false,"your message goes here"); }

yields this error message:

test.cpp: In function 'int main()' test.cpp:3: error: static assertion failed: "your message goes here"

The constant expression can of course depend on template parameters, and that's where the checking comes in: if you put a static_assert in a template then the compilation will fail if the specified condition is not True, and the error message will be a lot clearer than what you would get otherwise.

static_assert works really well with the standard type traits -- you can assert that a given type is convertible tobool, or is derived from a particular base class, or is a POD. Of course, you can also define your own traits for any characteristic that you can build a test for. Later in this article I'll show how you can build a test for the presence of particular member functions.

You could, for example enforce the constraint that a particular template parameter is a POD type, so you can copy instances with memcpy:

template<typename T> void copy(T const* source,T* dest,unsigned count) { static_assert(std::is_pod<T>::value,"T must be a POD"); memcpy(dest,source,count*sizeof(T)); } is

If you try and use this copy function with a non-POD type such as std::string, then you will get a compilation error.

The Boost Concept Check Library provides an alternative to static_assert for checking for concept conformance. Just like static_assert, it generates compiler errors if the concept is not matched. However, it may provide an easier way of specifying the constraints than plain static_assert if concept-based overloading is not required.

Whereas static_assert is all about hard and fast requirements, enable_if is about choices. You use it to enable certain function overloads if and only if a given property is True. This enables you to specify different versions of an algorithm based on the properties of the template parameters.

For example, you could use enable_if to use memcpy when copying PODs, rather than copying each element individually:

template<typename T> typename std::enable_if<std::is_pod<T>::value,void>::type copy(T const* source,T* dest,unsigned count) { memcpy(dest,source,count*sizeof(T)); } template<typename T> typename std::enable_if<!std::is_pod<T>::value,void>::type copy(T const* source,T* dest,unsigned count) { for(unsigned i=0;i<count;++i) { *dest++=*source++; } }

enable_if is quite simple in and of itself -- if the first template parameter (which must be a boolean constant expression) evaluates to true then the nested "type" member is a typedefto the second template parameter. If the the first parameter evaluates to False then there is no nested "type" member.

The SFINAE rules mean that if enable_if<some-expression,some-type>::type is used in the signature of a function template (as here), then that function overload is discarded if some-expression is False -- that overload is only enabled if some-expression is True.

SFINAEThe basic premise of SFINAE is that when the compiler is deducing the template parameters for a function template from a function call, if the deduced parameters would make the signature invalid then that function template is not considered for overload resolution rather than resulting in a compilation failure. There are limits, and some template instantiation errors will still cause compilation failure, but this is the basic principle that makes enable_ifwork.

For example, given the function template:

template<typename T> typename T::type foo(T t) {}

If you try and call foo(3), then T is deduced to be int. The type int does not have a member called type. The instantiation is therefore invalid when substituting "int" as the template parameter. By the SFINAE rule, this is not an error: instead the overload is ignored. If there is another overload of foo that can match foo(3), then that will be chosen instead. Of course, if there are no other overloads or none of the others match, then you still get a compilation error.

SFINAE does not work if the required instantiation of the function template depends on the instantiation of another template and that instantiation fails; for example:

template<typename T> struct bar { typedef typename T::type type; }; template<typename T> typename bar<T>::type foo2(T t) { // ... }

If you call foo2(3), then T is again deduced as int. However, bar<int> cannot be instantiated, since int does not have a member called type. This is a failure in the instantiation of bar<int>, not in the instantiation of foo2<int>, so is a real compiler error, and will not be ignored by SFINAE.


Availability of Features

Newer compilers (such as gcc 4.3 or later, and Microsoft Visual Studio 2010) are starting to provide C++0x features. static_assert is one of the most common C++0x language features added to compilers, but for those compilers that don't have static_assert, you can emulate it with BOOST_STATIC_ASSERT.

enable_if on the other hand is purely a library facility, and was part of the C++ Technical Report 1. As such it is even more widely available as part of the library supplied with compilers. For those compilers that don't provide their own version, it is also available as part of the Boost Library.

--A.W.

當前標題:static_assertenable_if模板編譯期檢查-創(chuàng)新互聯(lián)
本文路徑:http://aaarwkj.com/article34/dipise.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站全網(wǎng)營銷推廣、網(wǎng)頁設計公司、小程序開發(fā)網(wǎng)站策劃、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設
不卡二卡三卡四卡精品| 熟女亚洲一区精品久久| 在线观看精品日本一区二| 亚洲综合成人av在线| 国产精品一区二区三区久久| 97视频在线中文字幕| 欧美日韩视频一区二区| 亚洲日本成人av在线观看| av资源中文字幕在线天堂| 精品国产50部农村老熟女av| 欧美日韩美足一区二区| 极品美女粉嫩啪啪高潮| 国产三级精品av在线| 国产午夜福利一区在线| 国产综合一区二区三区视频| 色日韩在线观看视频| 亚洲日本中文字幕免费观看| 久久精品国产一区二区三| 一区二区三区毛片视频| 美日韩黄色大片免费看| 日韩在线视频观看一区二区三区| 在线观看不卡的黄色地址| 久久亚洲天堂av丁香| 正在播放蜜臀av在线| 国产亚洲精品精品国产亚洲| 日本一区二区三区三州免费观看| 大胆丰满邻居少妇在线观看| 高清日本一区二区三区不卡片| 麻豆乱淫一区二区三爱免费| 高潮少妇高潮少妇av| 国产l精品国产亚洲区久久| 成人黄色动漫在线播放| 亚洲国产成在人网站天堂| 亚洲一区二区三区 日韩精品| 精品亚洲一区二区三区四| 欧美成人精品三级一二| 日本区一区二区三视频| 麻豆人妻一区二区三区| 日韩精品熟妻人女亚洲一区 | 国产成人亚洲综合色影视| 国产三级精品三线在线观看 |