C++ 有没有一种方法可以定义一个静态函数
我有一个Singleton类,它只有我的MPIProxy类的一个实例(否则会导致很多构造) 要创建单个MPIProxy类,我有以下typedef:C++ 有没有一种方法可以定义一个静态函数,c++,typedef,C++,Typedef,我有一个Singleton类,它只有我的MPIProxy类的一个实例(否则会导致很多构造) 要创建单个MPIProxy类,我有以下typedef: typedef Singleton<MPIProxy_> MPIProxySingleton; 我更希望允许客户端进行编写,而不是简单地MPIProxySingleton::instance().xy()编写MPIProxy.xy()。其中.xy是MPIProxy_uuu类的函数。使上面的代码看起来像: int myrank = MPI
typedef Singleton<MPIProxy_> MPIProxySingleton;
我更希望允许客户端进行编写,而不是简单地MPIProxySingleton::instance().xy()
编写MPIProxy.xy()
。其中.xy是MPIProxy_uuu类的函数。使上面的代码看起来像:
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;
<代码> /Huff/戴维/Too/GSOC/MIn/MIn///LIBS/MPILib /I/Uptudio/MPIGyth.HPP:158:31:错误:C++需要所有声明的类型说明符
typedef Singleton::instance()MPIProxy;
~~~~~~~ ^
/Users/david/Desktop/GSOC/miind/miind//libs/MPILib/include/utilities/MPIProxy.hpp:158:41:错误:应为“;”后顶级声明器
typedef Singleton::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;
/**Singleton holder模板类。
*模板类来创建单例。类的单个实例
*MyType是使用创建和访问的
*\code
*typedef Singleton MySingletonType;
*MyType&myRef=MySingletonType::instance()
* // ... 做点什么。。。
*\endcode
*/
模板
单件阶级
{
公众:
//不允许创建、复制和分配
/**已删除构造函数以禁止显式构造。
*没有定义。
*/
Singleton()=删除;
/**已删除复制构造函数以禁止显式复制。
*没有定义。
*@param是一个单例对象。
*/
单例(const Singleton&S)=删除;
/**已删除分配运算符以禁止显式分配。
*@param是一个单例对象。
*@返回当前单例。
*/
单例和运算符=(常量单例和S)=删除;
/**返回对\c Singleton的唯一实例的引用。
*@返回对对象实例的引用。
*/
静态T&实例();
/**析构函数。
*/
~Singleton();
私人:
/**创建singleton实例(Meyers singleton,即。
*在第一次调用\c instance()时调用函数(静态对象)。
*/
静态void create();
/**指向实例的指针。
*/
静态T*pInstance_u2;;
/**单例的状态。如果单例已销毁,则为True。
*/
静态布尔;
};
/**返回类T的唯一实例。如果它已经存在
*已删除。将引发异常。如果从未使用过T类
*在生成新实例之前。
*
*@return类T的唯一实例
*/
模板T&Singleton::instance()
{
如果(!pInstance_u2;){
如果(已销毁){
//死引用
抛出异常(“实例已被销毁”);
}否则{
//初始创建
创建();
}
}
return*pInstance;
}
模板单例::~Singleton()
{
pInstance=0;
销毁=真;
}
模板void Singleton::create()
{
静态电阻;
pInstance_uu=&theInstance;
}
模板T*Singleton::pInstance_u0;
模板bool Singleton::destrocted_uz=false;
这里是我的MPIProxy_uu类的头
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;
class-MPIProxy\u{
公众:
/**
*析构函数
*/
虚拟代理;
/**
*用于返回进程id的包装器方法,如果禁用mpi,则返回0
*@返回进程的世界排名
*/
int getRank()常量;
/**
*包装器方法返回大小,如果禁用MPI,则返回1
*@返回
*/
int getSize()常量;
/**
*mpi屏障包装器
*/
空隙屏障();
/**
*等待向量中存储的所有请求完成
*/
void waitAll();
/**
*从根广播值
*@param value要广播的值
*@param root根进程
*/
模板
无效广播(T&value,int root);
/**
*异步接收操作mpi状态存储在mpi状态中
*@param source消息的来源
*@param标记消息的标记
*@param value接收到的值
*/
模板
无效irecv(整数源、整数标记、T和值)常数;
/**
*异步发送操作mpi状态存储在mpi状态中
*@param dest消息的目的地
*@param标记消息的标记
*@param value发送的值
*/
模板
void isend(int dest、int tag、const T和value)const;
私人:
/**
*将Singleton类声明为friend,以允许构造MPIProxy_uu类
*/
朋友班单身;
/**
*构造函数设置MPI等级和大小
*/
MPIProxy_u2;();
#ifdef启用\u MPI
/**
*存储mpi状态
*/
静态标准::向量状态;
#恩迪夫
/**
*存储列组以避免函数调用
*/
静态整数秩;
/**
*存储大小以避免函数调用
*/
静态整数大小;
};
模板
void MPIProxy::broadcast(T&value,int root){
#ifdef启用\u MPI
mpi::通信器世界;
boost::mpi::broadcast(世界、值、根);
#恩迪夫
}
模板
void MPIProxy\:irecv(int源、int标记、T和值)常量{
#ifdef启用\u MPI
mpi::通信器世界;
_mpiStatus.push_back(world.irecv(源、标记、值));
#否则
MPILib::utilities::Exception(“从irecv中的串行代码调用的MPI代码”);
#恩迪夫
}
模板
void MPIProxy\:isend(int dest、int tag、const T和value)const{
#ifdef启用\u MPI
mpi::通信器世界;
_mpiStatus.push_back(world.isend(dest,tag,value));
#否则
MPILib::utilities::Exception(“从isend中的串行代码调用的MPI代码”);
#恩迪夫
}
typedef单例MPIProxySingleton;
//typedef MPIProxySingleton::instance()MPIProxy;
引用它:
MPIProxy_& MPIProxy = MPIProxySingleton::instance();
参考它:
MPIProxy_& MPIProxy = MPIProxySingleton::instance();
<
MPIProxy_ & MPIProxy()
{
return MPIProxySingleton::instance();
}
MPIProxy().xy();
template <typename T>
class Singleton {
public:
static T & instance () {
static T instance_;
return instance_;
}
T * operator -> () const { return &instance(); }
};
class Foo_ {
friend class Singleton<Foo_>;
Foo_ () {}
public:
void bar () { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
typedef Singleton<Foo_> Foo;
Foo()->bar();