Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

A new way to implement Delegate in C++

, 25 May 2007
Solving issues with some current implementations of Delegate in C++
#pragma once

///////////////////////////////////////////////////////////////////////////////////
// COMPILE_TIME_JOIN macro
// Identical to BOOST_JOIN

#define COMPILE_TIME_JOIN(X, Y)		COMPILE_TIME_JOIN1(X, Y)
#define COMPILE_TIME_JOIN1(X, Y)	COMPILE_TIME_JOIN2(X, Y)
#define COMPILE_TIME_JOIN2(X, Y)	X##Y

///////////////////////////////////////////////////////////////////////////////////
// COMPILE_TIME_ASSERT macro

namespace compile_time_util
{

template<bool = true> class assert_class
{
public:
	class private_class_if_failed{};
};

template<> class assert_class<false>
{
private:
	class private_class_if_failed{};
};

}

#if defined(_MSC_VER)
#define COMPILE_TIME_ASSERT(expr) class COMPILE_TIME_JOIN(noname_struct_,__COUNTER__) : public compile_time_util::assert_class<(bool)(expr)>::private_class_if_failed {}
#else
#define COMPILE_TIME_ASSERT(expr) class COMPILE_TIME_JOIN(noname_struct_,__LINE__) : public compile_time_util::assert_class<(bool)(expr)>::private_class_if_failed {}
#endif


///////////////////////////////////////////////////////////////////////////////////
// OFFSET_OF macro
// A workaround to avoid problem with standard 'offsetof' when compiling with G++

#if defined(__GNUG__)
#define GCC_OFFSETOF_KEYWORD __offsetof__
#else
#define GCC_OFFSETOF_KEYWORD
#endif

#define OFFSET_OF(TYPE, MEMBER)							\
	(GCC_OFFSETOF_KEYWORD (reinterpret_cast <size_t>	\
	(&reinterpret_cast <const volatile char &>			\
	(reinterpret_cast<TYPE *> (1)->MEMBER))) - 1)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Quynh Nguyen
Global Cybersoft (Vietnam)
Vietnam Vietnam
Quynh Nguyen is a Vietnamese who has worked for 7 years in Software Outsourcing area. Currently, he works for Global Cybersoft (Vietnam) Ltd. as a Project Manager in Factory Automation division.
 
In the first day learning C language in university, he had soon switched to Assembly language because he was not able to understand why people cannot get address of a constant as with a variable. With that stupid starting, he had spent a lot of his time with Assembly language during the time he was in university.
 
Now he is interesting in Software Development Process, Software Architecture and Design Pattern… He especially indulges in highly concurrent software.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 25 May 2007
Article Copyright 2007 by Quynh Nguyen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid