小编jms*_*jms的帖子

字符串文字和constexpr数组之间的区别

我一直想知道如果有之间指出什么是任何差别ptrToArray,并ptrToLiteral在下面的例子:

constexpr char constExprArray[] = "hello";
const char* ptrToArray = constExprArray;

const char* ptrToLiteral = "hello";
Run Code Online (Sandbox Code Playgroud)
  • 我的理解是constExprArray和两个"hello"文字都是编译时间常数左值正确吗?
  • 如果是这样,它们在可执行文件中的存储方式有什么不同,还是纯粹是编译器实现或平台特定的?
  • 他们在幕后运行时是否有不同的待遇?
  • 还有什么需要了解的吗?

c++ string-literals constexpr c++11

12
推荐指数
1
解决办法
9857
查看次数

constexpr函数返回字符串文字

返回整数文字副本的函数

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)

c++ string c-strings constexpr c++11

7
推荐指数
1
解决办法
6831
查看次数

异构容器仅使用静态多态性

我的目标是实现一个容器(这里是一组堆栈,每种类型一个),同时接受许多不同类型的对象.在运行时,使用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++ containers templates metaprogramming c++11

6
推荐指数
1
解决办法
1636
查看次数

单例的共享指针不能互相识别

我正在学习如何使用C++ 11智能指针,同时使用SDL将2D游戏引擎编程为业余爱好.但是,我在为SDL实现OOp包装时遇到了问题.

目的是创建一个单例类,在构造时初始化SDL,并在销毁时关闭SDL.singleton类有一个静态方法getInstance,它返回一个shared_ptr单例,如果没有实例存在,则构造单个实例,这个想法是单例的所有客户端都拥有shared_ptr它,当所有客户端被销毁时,单例也被销毁.我确实理解单例(和其他全局变量)通常都很糟糕,但我认为这可能是少数单例适当的情况之一,因为只能使用一个SDL库.

问题在于shared_ptrgetInstance方法中返回.实例是不相关的,而不是使用相同的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)

c++ shared-ptr c++11

5
推荐指数
1
解决办法
6086
查看次数

检查类是否继承了模板的任何模板实例化

我编写了一个小实用程序,用于测试类型是否继承了特定模板类的某些模板实例,可以直接继承或继承继承模板的类.这是通过使用模板函数进行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)

c++ inheritance templates sfinae c++11

5
推荐指数
1
解决办法
1427
查看次数

C++:游戏引擎的事件系统实现

我正在计划一个事件驱动的游戏引擎.基本的想法是,不是让所有事情都与所有事情交谈,一切都会与事件系统进行对话,事件系统会将消息转发给收件人,而不会将收件人与通知者联系起来,反之亦然.

  • 对象将自己注册到事件系统.通知ID和指向回调函数的指针作为每个注册命令的参数传递.
  • 对象向事件系统添加通知.通知的ID作为每个通知的参数传递.通知将添加到包含所有待处理通知的队列中.
  • 此外,事件系统支持预定通知.通知ID和将来的执行时间作为每个通知的参数传递.然后将计划的通知存储在按照未来执行时间的顺序保存计划通知的数据结构("计划")中.
  • 调用者对象命令事件系统处理所有排队的通知.事件系统按顺序提取通知,并调用已使用与当前通知相同的ID注册自身的每个对象的回调函数.
  • 调用者对象命令事件系统处理一个预定的通知.从调度中获取最旧的通知,并调用使用相同通知ID注册的所有对象的回调.

.

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++ events game-engine

2
推荐指数
1
解决办法
7578
查看次数