小编Dyi*_*oul的帖子

在头文件中使用"using"

我明白了,我不应该在头文件中使用它:

using namespace foo;
Run Code Online (Sandbox Code Playgroud)

因为它为任何使用我的头文件的人带来了全局范围内的命名空间foo.

如果我在自己的命名空间中执行此操作,是否可以防止这种情况发生?例如这样:

namespace my_lib
{
    using namespace foo;

    // my stuff
    // ...
}
Run Code Online (Sandbox Code Playgroud)

现在使用命名空间foo应该限制在命名空间my_lib的范围内,对吧?

c++ namespaces using

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

针对非类型参数的特定值的模板代码优化.

template <bool flag>
class foo
{
    public:
        int bar()
        {
            if(flag)
            {
                // stuff
            }
        }
};
Run Code Online (Sandbox Code Playgroud)

当编译器编译此类时,它会将flag参数替换为true或false.然后我们有if(true)(或者if(false)).然后,if子句检查常量表达式并在编译时将被删除.我能指望编译器表现得像这样吗?

c++ templates boolean constants

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

仿函数调用(附加字符)

我试着构建一个最小的例子:

struct Functor
{
    void operator()(int& a)
    {
        a += 1;
    }
    void other(int& a)
    {
        a += 2;
    }
};

template <typename foo>
class Class
{
    public:
        void function()
        {
            int a = 10;
            foo()(a);
            std::cout << a << std::endl;
        }
};

int main()
{
    Class<Functor> c;
    c.function();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么甚至可以在没有对象的情况下调用纯类型的运算符?如何other以与调用相同的方式调用函数operator()

c++ call functor

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

标签 统计

c++ ×3

boolean ×1

call ×1

constants ×1

functor ×1

namespaces ×1

templates ×1

using ×1