我一直想知道如果有之间指出什么是任何差别ptrToArray,并ptrToLiteral在下面的例子:
constexpr char constExprArray[] = "hello";
const char* ptrToArray = constExprArray;
const char* ptrToLiteral = "hello";
Run Code Online (Sandbox Code Playgroud)
constExprArray和两个"hello"文字都是编译时间常数左值正确吗?返回整数文字副本的函数
int number()
{ return 1; }
Run Code Online (Sandbox Code Playgroud)
可以使用关键字轻松转换为普通的编译时表达式constexpr.
constexpr int number()
{ return 1; }
Run Code Online (Sandbox Code Playgroud)
但是,当涉及到字符串文字时,我会感到困惑.通常的方法是返回指向const char字符串文字的指针,
const char* hello()
{ return "hello world"; }
Run Code Online (Sandbox Code Playgroud)
但我认为仅仅改变"const" constexpr并不是我想要的(作为奖励,它还会产生编译器警告,使用gcc 4.7.1 将不推荐的从字符串常量转换为'char*')
constexpr char* hello()
{ return "hello world"; }
Run Code Online (Sandbox Code Playgroud)
有没有办法以hello()这样的方式实现调用在下面的示例中用常量表达式替换?
int main()
{
std::cout << hello() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我的目标是实现一个容器(这里是一组堆栈,每种类型一个),同时接受许多不同类型的对象.在运行时,使用void指针(或所有存储类型的公共基类)和运行时类型识别(RTTI),这将是微不足道的.由于容器将要保存的所有类型在编译时都是已知的,因此可能(或可能不)使用模板来创建这样的类.我知道boost::variant已经提供了类似的功能,但它要求存储的类型作为模板参数列出,如boost::variant< int, std::string > v;.
我真正想要的是一个类,每次push()创建等效的新模板特化时,它都会向自己透明地添加匹配(内部)数据结构.该类的用法如下所示:
int main()
{
MultiTypeStack foo;
//add a double to the container (in this case, a stack). The class would
//..create a matching std::stack<double>, and push the value to the top.
foo.push<double>(0.1);
//add an int to the container. In this case, the argument type is deduced.
//..The class would create a std::stack<int>, and push the value to the top.
foo.push(123);
//push a second double to the internal std::stack<double>.
foo.push<double>(3.14159); …Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用C++ 11智能指针,同时使用SDL将2D游戏引擎编程为业余爱好.但是,我在为SDL实现OOp包装时遇到了问题.
目的是创建一个单例类,在构造时初始化SDL,并在销毁时关闭SDL.singleton类有一个静态方法getInstance,它返回一个shared_ptr单例,如果没有实例存在,则构造单个实例,这个想法是单例的所有客户端都拥有shared_ptr它,当所有客户端被销毁时,单例也被销毁.我确实理解单例(和其他全局变量)通常都很糟糕,但我认为这可能是少数单例适当的情况之一,因为只能使用一个SDL库.
问题在于shared_ptr从getInstance方法中返回.实例是不相关的,而不是使用相同的shared_ptr管理器对象,shared_ptr并且销毁其中一个实例解除分配单例.
#include <iostream>
#include <memory>
using namespace std;
class Foo
{
public:
~Foo(){cout << "Foo <" << this << "> destroyed\n"; instance_ = nullptr;}
static shared_ptr<Foo> getInstance()
{
if(instance_ == nullptr)
instance_ = new Foo;
//problem: the shared pointers created are unaware of each other
return shared_ptr<Foo>(instance_);
}
private:
Foo(){cout << "Foo <" << this << "> constructed\n";}
Foo(Foo& other){}
void operator=(Foo& other){} …Run Code Online (Sandbox Code Playgroud) 我编写了一个小实用程序,用于测试类型是否继承了特定模板类的某些模板实例,可以直接继承或继承继承模板的类.这是通过使用模板函数进行SFINAE检查来完成的,模板函数接受所提供模板的任何模板实例化以及默认情况下的回退重载.
#include <iostream>
#include <type_traits>
template<template<class> class T, class U>
struct isDerivedFrom
{
static constexpr bool value = decltype(isDerivedFrom::test(U()))::value;
private:
template<class V>
static std::true_type test(T<V>);
static std::false_type test(...);
};
template<class T>
struct Base {};
struct Base_D1 : Base<int> {};
struct Base_D2 : Base<Base_D2> {};
struct Base_D1_D1 : Base_D1 {};
struct NotDerived {};
int main()
{
std::cout << std::boolalpha
<< "is Base_D1 derived from or a template instantiation of Base: "
<< isDerivedFrom<Base, Base_D1>::value << "\n"
<< "is Base_D2 derived from …Run Code Online (Sandbox Code Playgroud) 我正在计划一个事件驱动的游戏引擎.基本的想法是,不是让所有事情都与所有事情交谈,一切都会与事件系统进行对话,事件系统会将消息转发给收件人,而不会将收件人与通知者联系起来,反之亦然.
.
class Registration
{
public:
void callback(void){ callback_(); }
void setCallback((*callback)(void));
void addToEventSystem(int ID, EventSystem &eventSystem);
private:
void (*callback_)(void);
};
class EventSystem
{
public:
void register(int ID, Registration* registration);
void unRegister(int ID, Registration* registration);
void addNotificationToQueue(int ID);
void addNotificationToSchedule(int ID, int notificationTime);
void processQueuedNotifications(void);
void processNextScheduled(void);
int getCurrentTime(void);
private:
//placeholder types
<list> notificationQueue;
<binaryheap> notificationSchedule;
};
//------------Use:------------------
class ReceiverObject
{
public:
void doStuff(void);
void initialize(void){
keyPressRegistration.setCallback(doStuff);
//multiple registrations with different ID:s to same eventsystem …Run Code Online (Sandbox Code Playgroud) c++ ×6
c++11 ×5
constexpr ×2
templates ×2
c-strings ×1
containers ×1
events ×1
game-engine ×1
inheritance ×1
sfinae ×1
shared-ptr ×1
string ×1