'Library/C/C++'에 해당되는 글 6건
- 2008/10/10 CPPTMP Exercise 3-0
- 2008/09/25 CPPTM Exercise 2-4
- 2008/09/25 CPPTM Exercise 2-3
- 2008/09/25 CPPTM Exercise 2-2
- 2008/09/24 ADD_CONST_REF
- 2008/04/15 Hash 함수
1.4.1절에 나온 binary 팀플릿에, N에 0과 1 이외의 숫자가 들어 있을 때 binary<N>::value가 컴파일 오류를 일으키도록 하는 오류 점검을 BOOST_STATIC_ASSERT를 이용해서 추가하라.
code written by DamienRice.
#include <iostream>
#include <boost/static_assert.hpp>
using namespace std;
template <unsigned long N>
struct binary
{
BOOST_STATIC_ASSERT( (N % 10 < 2) );
static unsigned const value = binary<N/10>::value * 2 + N%10;
};
template<>
struct binary<0>
{
static unsigned const value = 0;
};
int main()
{
// BOOST_STATIC_ASSERT
cout << binary<101010>::value << endl; // ok
//cout << binary<10102>::value << endl; // error
//cout << binary<102011>::value << endl; // error
return 0;
}
#include <boost/static_assert.hpp>
using namespace std;
template <unsigned long N>
struct binary
{
BOOST_STATIC_ASSERT( (N % 10 < 2) );
static unsigned const value = binary<N/10>::value * 2 + N%10;
};
template<>
struct binary<0>
{
static unsigned const value = 0;
};
int main()
{
// BOOST_STATIC_ASSERT
cout << binary<101010>::value << endl; // ok
//cout << binary<10102>::value << endl; // error
//cout << binary<102011>::value << endl; // error
return 0;
}
code written by DamienRice.
여기서 발췌
이제 무언가 보이는 듯 하다. 그러나 아직도 멀고도 험한 길.
/* 과제 2-3(CPPTM Excercise 2.3)의 해답을, Boost형식 특질 라이브버리(type traits library)를 사용하지 않고 작성하라. 그리고 두 해답들을 비교하라 */
template<typename T, std::size_t N>
struct type_descriptor<T[N]>
{
std::ostream...
}
template<>
struct type_descriptor<int>
{
std::ostream& print(std::ostream &out) const
{
out << "int" << endl;
}
};
template<typename T, std::size_t N>
struct type_descriptor<T[N]>
{
std::ostream...
}
template<>
struct type_descriptor<int>
{
std::ostream& print(std::ostream &out) const
{
out << "int" << endl;
}
};
이제 무언가 보이는 듯 하다. 그러나 아직도 멀고도 험한 길.
여기서 발췌
발췌한 코드이긴 하지만, 내가 처음에 생각했던 방식과 같은 방법으로 구현되어 있다. RTTI와는 다르구나. 직접 Datatype-Typename 쌍을 지정해 주어야..
type_descriptor<long>()는 그냥 type_descriptor객체를 생성해 주는 것 뿐인건가?
template<typename T>
struct type_descriptor
{
ostream& print(ostream &out) const
{
if( boost::is_same<long, T>::value )
{
out << "long";
}
return out;
}
};
template<typename T>
std::ostream & operator<< (std::ostream & out, const type_descriptor<T> & desc)
{
return desc.print(out);
}
int main()
{
cout << type_descriptor<long>() << endl;
return 0;
}
struct type_descriptor
{
ostream& print(ostream &out) const
{
if( boost::is_same<long, T>::value )
{
out << "long";
}
return out;
}
};
template<typename T>
std::ostream & operator<< (std::ostream & out, const type_descriptor<T> & desc)
{
return desc.print(out);
}
int main()
{
cout << type_descriptor<long>() << endl;
return 0;
}
발췌한 코드이긴 하지만, 내가 처음에 생각했던 방식과 같은 방법으로 구현되어 있다. RTTI와는 다르구나. 직접 Datatype-Typename 쌍을 지정해 주어야..
type_descriptor<long>()는 그냥 type_descriptor객체를 생성해 주는 것 뿐인건가?
여기서 발췌. 다른 논의들도 있다.
/*boost::polymorhphic_downcast 함수 템플릿은 포인터를 다형적 객체로 하향 형변환하기
위한 static_cast의 점검 버전을 구현한다.
template<class Target, class Source>
inline Target polymorphic_downcast(Source *x)
{
assert(dynamic_cast<Target>(x) == x);
return static_cast<Target>(x);
}
릴리즈 모드에서는 assert가 사라지며, polymorphic_downcast는 보통의 static_cast만큼이나
효율적이 될 수 있다. 형식 특질 수단들을 이용해서, 이 템플릿을 포인터와 참조 인수 모두를
허용하도록 구현하라.
struct A {virtual ~A(){} };
struct B : A{};
B b;
A *a_ptr = &b;
B* b_ptr = polymorphic_downcast<B*>(a_ptr);
A& a_ref = b;
B& b_ref = polymorphic_downcast<B&>(a_ref);
*/
#include <cassert>
#include <boost/type_traits/remove_reference.hpp>
struct A {virtual ~A(){} };
struct B : A {};
template<class Target, class Source>
inline Target polymorphic_downcast(Source *x)
{
assert(dynamic_cast<Target>(x) == x );
return static_cast<Target>(x);
}
template<class Target, class Source>
inline Target polymorphic_downcast(Source &x)
{
typedef typename boost::remove_reference<Target>::type noref;
assert(dynamic_cast<noref*>(&x) == &x );
return static_cast<Target>(x);
}
int main()
{
B b;
A *a_ptr = &b;
B* b_ptr = polymorphic_downcast<B*>(a_ptr);
A& a_ref = b;
B& b_ref = polymorphic_downcast<B&>(a_ref);
return 0;
}
위한 static_cast의 점검 버전을 구현한다.
template<class Target, class Source>
inline Target polymorphic_downcast(Source *x)
{
assert(dynamic_cast<Target>(x) == x);
return static_cast<Target>(x);
}
릴리즈 모드에서는 assert가 사라지며, polymorphic_downcast는 보통의 static_cast만큼이나
효율적이 될 수 있다. 형식 특질 수단들을 이용해서, 이 템플릿을 포인터와 참조 인수 모두를
허용하도록 구현하라.
struct A {virtual ~A(){} };
struct B : A{};
B b;
A *a_ptr = &b;
B* b_ptr = polymorphic_downcast<B*>(a_ptr);
A& a_ref = b;
B& b_ref = polymorphic_downcast<B&>(a_ref);
*/
#include <cassert>
#include <boost/type_traits/remove_reference.hpp>
struct A {virtual ~A(){} };
struct B : A {};
template<class Target, class Source>
inline Target polymorphic_downcast(Source *x)
{
assert(dynamic_cast<Target>(x) == x );
return static_cast<Target>(x);
}
template<class Target, class Source>
inline Target polymorphic_downcast(Source &x)
{
typedef typename boost::remove_reference<Target>::type noref;
assert(dynamic_cast<noref*>(&x) == &x );
return static_cast<Target>(x);
}
int main()
{
B b;
A *a_ptr = &b;
B* b_ptr = polymorphic_downcast<B*>(a_ptr);
A& a_ref = b;
B& b_ref = polymorphic_downcast<B&>(a_ref);
return 0;
}
여기서 퍼옴
한눈에 봐도 참 잘 작성한 코드이다.
add_reference, add_const를 include를 왜 했는지 모르겠다. 밑바닥이 없으니 모든것이 생소하구나.
/* From C++ Template Metaprogramming Exercise 2.0:
Write a unary metafunction add_const_ref<T> that returns T
if it is a reference type, and otherwise returns T const&.
Write a program to test your metafunction. Hint: you can
use boost::is_same to test the results.
*/
#include <iostream>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/add_const.hpp>
using namespace std;
using namespace boost;
template <class T, bool is_ref>
struct add_const_ref_helper;
template <class T>
struct add_const_ref
{
typedef typename
add_const_ref_helper<T, is_reference<T>::value>::type type;
};
template <class T>
struct add_const_ref_helper<T, true>
{
typedef T type;
};
template <class T>
struct add_const_ref_helper<T, false>
{
typedef T const& type;
};
int main()
{
cout << "Reference: "
<< is_same<add_const_ref<int&>::type, int&>::value
<< endl;
cout << "Non-Reference: "
<< is_same<add_const_ref<int>::type, const int&>::value
<< endl;
return EXIT_SUCCESS;
}
Write a unary metafunction add_const_ref<T> that returns T
if it is a reference type, and otherwise returns T const&.
Write a program to test your metafunction. Hint: you can
use boost::is_same to test the results.
*/
#include <iostream>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/add_const.hpp>
using namespace std;
using namespace boost;
template <class T, bool is_ref>
struct add_const_ref_helper;
template <class T>
struct add_const_ref
{
typedef typename
add_const_ref_helper<T, is_reference<T>::value>::type type;
};
template <class T>
struct add_const_ref_helper<T, true>
{
typedef T type;
};
template <class T>
struct add_const_ref_helper<T, false>
{
typedef T const& type;
};
int main()
{
cout << "Reference: "
<< is_same<add_const_ref<int&>::type, int&>::value
<< endl;
cout << "Non-Reference: "
<< is_same<add_const_ref<int>::type, const int&>::value
<< endl;
return EXIT_SUCCESS;
}
한눈에 봐도 참 잘 작성한 코드이다.
add_reference, add_const를 include를 왜 했는지 모르겠다. 밑바닥이 없으니 모든것이 생소하구나.
MUL = 31; // OR 37.. 둘다 소수이다.
uint hash(char *str)
{
uint h;
uchar *p;
for each p in str
h = MUL*h + uint(p)
return h mod NUMARR; // NUMARR은 소수로.
}
uint hash(char *str)
{
uint h;
uchar *p;
for each p in str
h = MUL*h + uint(p)
return h mod NUMARR; // NUMARR은 소수로.
}

Prev
Rss Feed