any 는 마치 스크립트 언어처럼 임의의 type을 가지는 변수를 만들 수 있다.
COM의 Varient 와도 비슷하다 하겠다.

하지만 이것을 언어수준에서 구현 해 놓았고, 다른 컨테이너와 매우 잘 맞물린다는것이 장점이라 하겠다.
vector 에 이런저런 잡데이터 쑤셔넣을때 참 좋더라
(당연히 오버헤드는 있다. 꼭 필요한 곳에만 사용하자!)

#include "stdafx.h"
#include <cstring>
#include <cstdio>
#include <boost/any.hpp>
#include <string>
#include <vector> 

using namespace boost;

using namespace std;

 

struct MyData

{

        int i;

        double d;

        MyData()

        {

        }

        MyData(const MyData& t) //Any의요구사항1 Copy Constructor

        {

               this->i = t.i;

               this->d = t.d;

        }

        MyData& operator =(const MyData &t) //Any의요구사항2 = operator overloading

        {

               this->i = t.i;

               this->d = t.d;

        }

};

int _tmain(int argc, _TCHAR* argv[])

{

        any Data;

        //값이비어있는지를확인하는방법

        if(Data.empty())

        {

               puts("Data 는비어있음!");

        }

        else

        {

               puts("Data 는비어있지않음!");

        }

        Data = 3.14; //실수리터럴의기본타잎은double

        Data = 3; //정수리터럴의기본타잎은Int

 

        //Data = "Hello!"; //에러.

        //any 에넣기위해선CopyConstructor = Operator Overloading 이필요하다.

        string str = "Hello";

        Data = str; //정상적

 

        //Any 에어떤타잎이들어있는지확인

        //type매소드로type 을확인후any_cast 연산자를사용하여캐스팅

        if(Data.type() == typeid(int))

        {

               printf("DataType : Int, Value : %d\n", any_cast<int>(Data));

        }

        else if(Data.type() == typeid(double))

        {

               printf("DataType : Double, Value : %lf\n", any_cast<double>(Data));

        }

        else if(Data.type() == typeid(string))

        {

               printf("DataType : string, Value : %s\n", any_cast<string>(Data).c_str());

        }

 

        //Vector Any 의복합사용예.

        printf("임의의타잎을담는Vector 선언하기\n");

        vector<any> AnyVector;

        MyData myData;

        myData.d = 5.12;

        myData.i = 5;

 

        AnyVector.push_back(3);

        AnyVector.push_back(3.14);

        AnyVector.push_back(myData);

 

        vector<any>::iterator it;

        for (it = AnyVector.begin(); it != AnyVector.end(); ++it)

        {

               if(it->type() == typeid(int))

               {

                       printf("Any Type : Int, Value : %d\n", any_cast<int>(*it));

               }

               else if(it->type() == typeid(double))

               {

                       printf("Any Type : Double, : %lf\n", any_cast<double>(*it));

               }

               else if(it->type() == typeid(MyData))

               {

                       printf("Any Type : MyData, Value : %d, %lf\n", any_cast<MyData>(*it).i, any_cast<MyData>(*it).d);

               }

        }

        return 0;

}

 

 

조회 수 :
13844
등록일 :
2009.05.11
13:44:03 (*.149.217.155)
엮인글 :
http://www.lunapiece.net/3793/84d/trackback
게시글 주소 :
http://www.lunapiece.net/3793
문서 첨부 제한 : 0Byte/ 10.00MB
파일 제한 크기 : 10.00MB (허용한 확장자만 첨부할 수 있습니다.(관리자는 제외) : *.*)
옵션 :
:
:
:
:
List of Articles
번호 제목 글쓴이 날짜 조회 수
13 [Boost 살펴보기] 7. String Algorithm2 Lyn 2009-05-20 14374
12 [Boost 살펴보기] 6. String Algorithm1 Lyn 2009-05-12 15375
11 [Boost 살펴보기] 5. lexical_cast file Lyn 2009-05-12 15661
10 [Boost 살펴보기] 4. multi_array Lyn 2009-05-11 14897
9 [Boost 살펴보기] 3. timer Lyn 2009-05-11 14905
» [Boost 살펴보기] 2. any Lyn 2009-05-11 13844
7 [Boost 살펴보기] 1. pool file Lyn 2009-05-11 14455
6 프로그래밍 대회 알고리즘 파트 문제 [2] secret Lyn 2008-11-21 15485
5 컨테이너가 파괴될 때 소유한 객체 자동으로 파괴하기 Lyn 2008-10-22 13210
4 [TR1 살펴보기] 3. UnOrdered Containers Lyn 2008-10-05 14319
3 [TR1 살펴보기] 2. Array Lyn 2008-10-05 14256
2 [TR1 살펴보기] 1. Random Lyn 2008-10-05 14696
1 Delphi 2009 Generic 살펴보기 [1] Lyn 2008-09-29 12696

SITE LOGIN :)