如果返回类型是C ++中的对象,我们可以在函数定义中使用绝对名称空间吗? [英] Can we have absolute namespace in function definition if return type is an object in C++?

查看:110
本文介绍了如果返回类型是C ++中的对象,我们可以在函数定义中使用绝对名称空间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑在命名空间 foo 中声明的函数 bar ,该函数返回 std: :vector< float> (但也可以与其他对象一起使用)。

Let us consider a function bar declared in namespace foo which returns a std::vector< float > (but also works with other objects).

// header.h
#include <vector>

namespace foo
{
        ::std::vector< float > bar();
}

使用相对名称空间工程编译其定义。

Compiling its definition using relative namespace works.

#include "header.h"

::std::vector< float > foo::bar()
{
}

但是,编译其定义使用绝对名称空间不起作用。

However, compiling its definition using absolute namespace does not work.

#include "header.h"

::std::vector< float > ::foo::bar()
{
}

返回错误GCC是

function.cpp:3:26: error: ‘foo’ in ‘class std::vector<float>’ does not name a type
::std::vector< float > ::foo::bar()

事实证明,命名空间中允许使用空格,因此 :: std :: vector<浮动> :: foo :: bar()等效于 :: std :: vector<浮动> :: foo :: bar()。返回类型为对象时,如何在函数定义中使用绝对名称空间?

It turns out that spaces are allowed in namespacing, so, ::std::vector< float > ::foo::bar() is equivalent to ::std::vector< float >::foo::bar(). How can I use absolute namespace in function definition when return type is an object?

推荐答案

::std::vector< float > (::foo::bar)()
{
    // stuff
}

这篇关于如果返回类型是C ++中的对象,我们可以在函数定义中使用绝对名称空间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆