有没有一种方法可以对静态函数进行类型定义
Is there a way to typedef a static function
我有一个Singleton类,它只有MPIProxy类的一个实例(否则会导致许多构造)。
要创建单个MPIProxy类,我有以下typedef:
typedef Singleton<MPIProxy_> MPIProxySingleton;
然而,要访问实例,我需要以下代码:
int myrank = MPIProxySingleton::instance().getRank();
我更希望允许客户端进行写入,而不是简单地使用MPIProxySingleton::instance().xy()
和MPIProxy.xy()
。其中.xy是MPIProxy_类的函数。这样上面的代码看起来像:
int myrank = MPIProxy.getRank();
我试过以下方法:
typedef MPIProxySingleton::instance() MPIProxy;
然而,这会导致编译错误:
/Users/david/Desktop/GSOC/miind/miind/./libs/MPILib/include/utilities/MPIProxy.hpp:158:31: error: C++ requires a type specifier for all declarations
typedef Singleton<MPIProxy_>::instance() MPIProxy;
~~~~~~~ ^
/Users/david/Desktop/GSOC/miind/miind/./libs/MPILib/include/utilities/MPIProxy.hpp:158:41: error: expected ';' after top level declarator
typedef Singleton<MPIProxy_>::instance() MPIProxy;
这是我的Singleton类的代码:
/** Singleton holder template class.
* Template class to create singletons. A singleton instance of class
* MyType is created and accessed using
* code
* typedef Singleton<MyType> MySingletonType;
* MyType& myRef = MySingletonType::instance()
* // ... do something ...
* endcode
*/
template<class T>
class Singleton
{
public:
// disallow creation, copying and assignment
/** Deleted constructor to disallow explicit construction.
* Is not defined.
*/
Singleton()=delete;
/** Deleted copy constructor to disallow explicit copying.
* Is not defined.
* @param S A singleton object.
*/
Singleton(const Singleton& S)=delete;
/** Deleted assignment operator to disallow explicit assignment.
* @param S A singleton object.
* @return The current singleton.
*/
Singleton& operator=(const Singleton& S)=delete;
/** Return a reference to the only instance of c Singleton<T>.
* @return A reference to the instance of the object.
*/
static T& instance();
/** Destructor.
*/
~Singleton();
private:
/** Create method. Creates the singleton instance (a Meyers singleton, ie.
* a function static object) upon the first call to c instance().
*/
static void create();
/** Pointer to the instance.
*/
static T* pInstance_;
/** Status of the singleton. True if the singleton was destroyed.
*/
static bool destroyed_;
};
/** Returns the unique instance of class T. If it was already
* deleted an exception is thrown. If the class T was never used
* before a new instance is generated.
*
* @return Unique instance of class T
*/
template<class T> T& Singleton<T>::instance()
{
if (!pInstance_) {
if (destroyed_) {
// dead reference
throw Exception("The instance was already destroyed");
} else {
// initial creation
create();
}
}
return *pInstance_;
}
template<class T> Singleton<T>::~Singleton()
{
pInstance_ = 0;
destroyed_ = true;
}
template<class T> void Singleton<T>::create()
{
static T theInstance;
pInstance_ = &theInstance;
}
template<class T> T* Singleton<T>::pInstance_ = 0;
template<class T> bool Singleton<T>::destroyed_ = false;
这里是我的MPIProxy_类的头
class MPIProxy_ {
public:
/**
* destructor
*/
virtual ~MPIProxy_();
/**
* wrapper method to return the process id, if mpi is disabled it returns 0
* @return the world rank of a process
*/
int getRank() const;
/**
* wrapper method to return the size, if MPI is disabled it returns 1
* @return
*/
int getSize() const;
/**
* wrapper for mpi barrier
*/
void barrier();
/**
* waits until all request stored in the vector _mpiStatus are finished
*/
void waitAll();
/**
* Broadcast the value from root
* @param value The value to be broadcast
* @param root The root process
*/
template<typename T>
void broadcast(T& value, int root);
/**
* asynchronous receive operation the mpi status is stored in _mpiStatus
* @param source The source of the message
* @param tag The tag of the message
* @param value The value received
*/
template<typename T>
void irecv(int source, int tag, T& value) const;
/**
* asynchronous send operation the mpi status is stored in _mpiStatus
* @param dest The destination of the message
* @param tag The tag of the message
* @param value The value sended
*/
template<typename T>
void isend(int dest, int tag, const T& value) const;
private:
/**
* Declare the Singleton class a friend to allow construction of the MPIProxy_ class
*/
friend class Singleton<MPIProxy_>;
/**
* constructor sets the MPI rank and size
*/
MPIProxy_();
#ifdef ENABLE_MPI
/**
* stores the mpi statuses
*/
static std::vector<boost::mpi::request> _mpiStatus;
#endif
/**
* storage of the rank to avoid function calls
*/
static int _rank;
/**
* storage of the size to avoid function calls
*/
static int _size;
};
template<typename T>
void MPIProxy_::broadcast(T& value, int root) {
#ifdef ENABLE_MPI
mpi::communicator world;
boost::mpi::broadcast(world, value, root);
#endif
}
template<typename T>
void MPIProxy_::irecv(int source, int tag, T& value) const {
#ifdef ENABLE_MPI
mpi::communicator world;
_mpiStatus.push_back(world.irecv(source, tag, value));
#else
MPILib::utilities::Exception("MPI Code called from serial code in irecv");
#endif
}
template<typename T>
void MPIProxy_::isend(int dest, int tag, const T& value) const {
#ifdef ENABLE_MPI
mpi::communicator world;
_mpiStatus.push_back(world.isend(dest, tag, value));
#else
MPILib::utilities::Exception("MPI Code called from serial code in isend");
#endif
}
typedef Singleton<MPIProxy_> MPIProxySingleton;
//typedef MPIProxySingleton::instance() MPIProxy;
在这种情况下,包装函数总是一个不错的选择:
MPIProxy_ & MPIProxy()
{
return MPIProxySingleton::instance();
}
然后将其用作:
MPIProxy().xy();
我只是想用一种稍微不同的方式来完成同样的事情。由于Singleton
被认为是无状态的,因此允许Singleton
被实例化、复制、分配或销毁实际上没有害处。它只是模板参数的单个静态实例的包装器。考虑到这一点,您可以这样定义Singleton
:
template <typename T>
class Singleton {
public:
static T & instance () {
static T instance_;
return instance_;
}
T * operator -> () const { return &instance(); }
};
因此,对于要制作Singleton
:的对象
class Foo_ {
friend class Singleton<Foo_>;
Foo_ () {}
public:
void bar () { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
typedef Singleton<Foo_> Foo;
然后,你可以这样使用它:
Foo()->bar();
引用它:
MPIProxy_& MPIProxy = MPIProxySingleton::instance();
相关文章:
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 为什么静态成员函数定义不能有关键字"static"?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- Cmake,Android:调用在预构建库中定义的静态函数
- 如何定义从虚拟函数继承的静态函数
- C++:静态成员不能在声明时定义,但静态函数变量可以?
- 从库中定义的进程调用静态函数
- 有没有一种方法可以对静态函数进行类型定义
- 类外静态函数的声明不是定义
- 一个定义规则是否强制创建一个静态函数变量
- 将函数定义为静态成员和自由成员之间有什么区别
- 在C++中声明但未定义的静态函数
- 标头中的静态函数定义
- 取消定义静态函数的引用错误
- 使用 GMOCK 模拟在 .cpp 中声明和定义的静态函数,而无需类文件
- 对静态函数的未定义引用
- 我的静态函数header和.cpp是怎么回事?仅在标头中定义时运行
- 引用静态函数作为类定义的一部分是否有任何开销?
- 在匿名命名空间内部和外部定义静态函数有什么区别吗?
- c++ typedef静态函数指针:未定义符号