小编Jam*_*mes的帖子

Ruby vs Lua作为C++的脚本语言

我目前正在构建一个游戏服务器(不是引擎),我希望它可以扩展,就像一个插件系统.
我找到的解决方案是使用脚本语言.到现在为止还挺好.

我不确定我是否应该使用Ruby或Lua.Lua更容易嵌入,但Ruby有一个更大的库,更好的语法(在我看来).问题是,我没有找到使用Ruby作为C++脚本语言的简单方法,而使用Lua则非常容易.

对此有些不满?使用Ruby作为脚本语言的建议(我试过SWIG,但它不像使用Lua那样整洁)?

谢谢.

c++ ruby lua

23
推荐指数
3
解决办法
1万
查看次数

C++ - 迭代元组和类型与常量参数的分辨率

我目前正在为元组编写算术运算符重载.运算符迭代元组以对其每个元素执行操作.这是operator + =的定义:

template< typename... Ts, std::size_t I = 0 >
inline typename std::enable_if< I == sizeof... (Ts), std::tuple< Ts... >& >::type operator +=(std::tuple< Ts... >& lhs, const std::tuple< Ts... >& rhs)
{
    return lhs;
}

template< typename... Ts, std::size_t I = 0 >
inline typename std::enable_if< I != sizeof... (Ts), std::tuple< Ts... >& >::type operator +=(std::tuple< Ts... >& lhs, const std::tuple< Ts... >& rhs)
{
    std::get< I >(lhs) += std::get< I >(rhs);
    return operator +=< Ts..., I + 1 …
Run Code Online (Sandbox Code Playgroud)

c++ tuples enable-if c++11

11
推荐指数
1
解决办法
6203
查看次数

带有默认值的模板返回类型

所以在编写C++模板类时,我已经定义了一个返回模板化类型对象的方法,如下所示:

template <typename T>
class Foo
{
public:
    T GetFoo()
    {
        T value;

        //Do some stuff that might or might not set the value of 'value'

        return value;
    }
 };

int main() {

    Foo<int> foo;

    foo.GetFoo();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这给出了以下警告:

prog.cpp: In member function ‘T Foo<T>::GetFoo() [with T = int]’:
prog.cpp:15: warning: ‘value’ is used uninitialized in this function
Run Code Online (Sandbox Code Playgroud)

我明白为什么会发生这种情况 - 我将返回一个未初始化int的部分GetFoo.问题是,如果我要使用Foo<SomeClass>,该行将使用默认构造函数T value;初始化.valueSomeClass

我设法通过执行以下操作来抑制此警告:

    T GetFoo()
    {
        T value = …
Run Code Online (Sandbox Code Playgroud)

c++ templates

4
推荐指数
1
解决办法
9449
查看次数

Isnt setprecision不应该改变存储在变量中的值吗?

我认为setprecision不会改变变量本身的值.此外,当您将setprecision附加到cout时,它只会粘贴一次.但是,当我运行代码来验证时,它不起作用.

请考虑以下代码段:

int main()
{
    double x = 9.87654321;
    cout << setprecision(3) << fixed << x <<endl;    //Returns 9.877 as it should
    cout << x << endl;                               //Returns truncated value 9.877 again though it shouldnt.

    return 0;
}   
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我们用cout << x << endl;线设置精度替换为7,那么它会显示正确的值.谁能解释一下这个现象吗?

c++

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

包含类定义的模板容器,只需要多个模板参数之一

这与本文下方提出的问题略有不同.假设我有一个容器类,它有两个模板参数,第一个是类型,第二个是容器的大小.

现在我们有多个具有不同容器存储大小的容器.从本质上讲,容器功能(所有公共功能)只是真正关心T; N仅用于分配本地存储(如果N不够,则使用分配器).

我已经汇总了一个简单的示例实现,展示了我遇到的问题.

#include <iostream>

template <typename T, size_t N = 10>
class TestArray
{
public:
    T Local[N];

    class Iterator
    {
    public:
        T* Array;
        int Index;      

        Iterator() : Array(NULL), Index(-1) { }
        Iterator(T* _array, int _index) : Array(_array), Index(_index) { }

        bool operator == (const Iterator& _other) const 
        { 
             return _other.Index == Index && _other.Array == Array; 
        }

        bool operator != (const Iterator& _other) const 
        { 
            return !(*this == _other); 
        }

        template <size_t _N> …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

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

调试指针算术

当我打印变量地址之间的差异时,我无法解决为什么我找到差异

这是代码:

int main()
{

        int *a,b = 5,e = 0;
        int *c,d = 10,f = 0;
        long t1,t2;
        a = &b;
        c = &d;
        e = &b;
        f = &d;
        t1 = e - f;
        t2 = a - c;
       printf("\n Address of b using a: %x \t %d using e : %x \t %d value of b : %d",a,a,e,e,b);
       printf("\n Address of d using c: %x \t %d using f : %x \t %d value of d : …
Run Code Online (Sandbox Code Playgroud)

c pointers

0
推荐指数
1
解决办法
448
查看次数

需要帮助函数调用

这是我学习IT和c ++的第一年,所以我对c ++没有太多的了解.问题是关于函数调用.

功能标题是:

bool insure(int age, bool smoker)
Run Code Online (Sandbox Code Playgroud)

我的任务是写下正确的函数调用并声明我将在调用语句中使用的所有变量.

到目前为止,这是我提出的:

#include <iostream>
using namespace std;

bool insure(int age, bool smokers)
{
    bool ret;
}
int main ()
{
    int age;
    bool isSmoker;
    bool ret;

    cout << "Enter your age: ";
    cin >> age;
    ret = insure(age, isSmoker);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道这个程序是否正确或者我做错了什么.提前致谢

c++

0
推荐指数
1
解决办法
145
查看次数

标签 统计

c++ ×6

templates ×2

c ×1

c++11 ×1

enable-if ×1

h.264 ×1

jpeg ×1

lua ×1

pointers ×1

ruby ×1

tuples ×1

video ×1