相关疑难解决方法(0)

C++ 11:如何为函数设置别名?

如果我在命名空间栏中有一个类Foo:

namespace bar
{
    class Foo { ... }
};
Run Code Online (Sandbox Code Playgroud)

那我可以:

using Baz = bar::Foo;
Run Code Online (Sandbox Code Playgroud)

现在就像我在名称空间中使用名称Baz定义类一样.

是否有可能为功能做同样的事情?

namespace bar
{
    void f();
}
Run Code Online (Sandbox Code Playgroud)

然后:

using g = bar::f; // error: ‘f’ in namespace ‘bar’ does not name a type
Run Code Online (Sandbox Code Playgroud)

最干净的方法是什么?

解决方案还应该适用于模板功能.

定义:如果某个实体B是A 的别名,那么与源代码中的B替换A的任何或所有用法(当然不是声明​​或定义)相比,(剥离的)生成的代码保持不变.例如typedef A B是别名. #define B A是一个别名(至少). T& B = A不是别名,B可以有​​效地实现为间接指针,一个"unaliased"A可以使用"立即语义".

c++ linux gcc c++11

61
推荐指数
6
解决办法
3万
查看次数

别名可变参数模板函数

我有一个可变函数,如:

void test(int){}

template<typename T,typename...Args>
void test(int& sum,T v,Args... args)
{
    sum+=v;
    test(sum,args...);
}
Run Code Online (Sandbox Code Playgroud)

我想将它别名为:

auto sum = test;//error : can not deduce auto from test
int main()
{
    int res=0;
    test(res,4,7);
    std::cout<<res;
}
Run Code Online (Sandbox Code Playgroud)

我尝试过使用std::bind但是它不适用于可变参数函数,因为它需要占位符...

是否可以为可变参数函数添加别名?

c++ templates variadic-templates c++11 c++14

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

标签 统计

c++ ×2

c++11 ×2

c++14 ×1

gcc ×1

linux ×1

templates ×1

variadic-templates ×1