问题描述
以下代码(其中嵌套类Info
将外部类Impl
的两个成员函数指定为朋友)使用以下代码在Visual C ++和g ++中很好地编译.
但是,如果没有在Info
之前声明成员函数,则g ++会抱怨不完整类,而不是缺少声明:
In file included from ../console_io.hpp:6:0, from console_io.cpp:1: ../console_io.impl.windows.hpp:69:47: error: invalid use of incomplete type 'class console::Display::Impl' friend auto Impl::api_info() const -> Ref_<const Api_info>; ^ ../console_io.impl.windows.hpp:62:20: note: definition of 'class console::Display::Impl' is not complete until the closing brace class Display::Impl ^ ../console_io.impl.windows.hpp:70:47: error: invalid use of incomplete type 'class console::Display::Impl'
最小示例:
struct Foo
{
class Bar
{
friend void Foo::m();
};
void m(){}
};
foo.cpp:5:28: error: invalid use of incomplete type 'struct Foo' friend void Foo::m(); ^ foo.cpp:1:8: note: definition of 'struct Foo' is not complete until the closing brace struct Foo ^
因此,通过关联,我想知道在T
尚未完成的时候,将T
的成员函数设为friend
的形式有效性.
我提供了编译的实际原始代码,因此,如果它在形式上无效,则可以为该用例提出合理的替代方案.我认为,作为问的那个人,我可能是最无能力决定与答案相关或不相关的人.表示法:Ref_<T>
表示T&
.
class Display::Impl
{
private:
using Api_info = impl::winapi::Console_screen_buffer_info;
inline auto api_info() const -> Ref_<const Api_info>; // Def. after class.
inline void invalidate_api_info(); // Def. after class.
class Info
{
friend auto Impl::api_info() const -> Ref_<const Api_info>;
friend void Impl::invalidate_api_info();
private:
bool is_valid_ = false;
Api_info api_info_ = {};
};
// State:
impl::winapi::Handle text_buffer_;
mutable Info info_;
Impl( Ref_<const Impl> ) = delete;
auto operator=( Ref_<const Impl> ) -> Ref_<Impl> = delete;
public:
auto size() const
-> Point
{
auto const api_size = api_info().dwSize;
return Point{ api_size.x, api_size.y };
}
~Impl()
{} // TODO:
Impl( const Point size )
: text_buffer_( impl::winapi::CreateConsoleScreenBuffer(
impl::winapi::flag_GENERIC_READ | impl::winapi::flag_GENERIC_WRITE,
0, // No sharing
nullptr, // Default security.
impl::winapi::flag_CONSOLE_TEXTMODE_BUFFER, // The allowed value.
nullptr // Reserved.
) )
{
hopefully( text_buffer_ != impl::winapi::invalid_handle_value )
|| fail( "console::Display: CreateConsoleScreenBuffer failed" );
}
};
auto Display::Impl::api_info() const
-> Ref_<const Api_info>
{
if( not info_.is_valid_ )
{
impl::winapi::GetConsoleScreenBufferInfo( text_buffer_, &info_.api_info_ )
|| fail( "GetConsoleScreenBufferInfo failed" );
info_.is_valid_ = true;
}
return info_.api_info_;
}
void Display::Impl::invalidate_api_info()
{ info_.is_valid_ = false; }
经过一番挖掘,我认为这是您想要的:
§ 9.3 [class.mfct]:
因此(据我了解的标准),当您在嵌套类之前声明成员函数时,您的代码有效.
The following code, where the nested class Info
designates two member functions of the outer class Impl
as friends, compiles nicely with Visual C++ and g++, with the code as given below.
But, if the member functions aren't declared before Info
, then g++ complains about incomplete class rather than missing declaration:
In file included from ../console_io.hpp:6:0, from console_io.cpp:1: ../console_io.impl.windows.hpp:69:47: error: invalid use of incomplete type 'class console::Display::Impl' friend auto Impl::api_info() const -> Ref_<const Api_info>; ^ ../console_io.impl.windows.hpp:62:20: note: definition of 'class console::Display::Impl' is not complete until the closing brace class Display::Impl ^ ../console_io.impl.windows.hpp:70:47: error: invalid use of incomplete type 'class console::Display::Impl'
Minimal example:
struct Foo
{
class Bar
{
friend void Foo::m();
};
void m(){}
};
foo.cpp:5:28: error: invalid use of incomplete type 'struct Foo' friend void Foo::m(); ^ foo.cpp:1:8: note: definition of 'struct Foo' is not complete until the closing brace struct Foo ^
And so by association I wonder about the formal validity of making a member function of T
a friend
, at a point where T
is not yet complete.
I present the actual original code that compiles, so that, if it's formally invalid, reasonable alternatives for this use case can be suggested. I think that I, as the one who's asking, am possibly the least competent to decide what's relevant or not for answers. Notation: Ref_<T>
means T&
.
class Display::Impl
{
private:
using Api_info = impl::winapi::Console_screen_buffer_info;
inline auto api_info() const -> Ref_<const Api_info>; // Def. after class.
inline void invalidate_api_info(); // Def. after class.
class Info
{
friend auto Impl::api_info() const -> Ref_<const Api_info>;
friend void Impl::invalidate_api_info();
private:
bool is_valid_ = false;
Api_info api_info_ = {};
};
// State:
impl::winapi::Handle text_buffer_;
mutable Info info_;
Impl( Ref_<const Impl> ) = delete;
auto operator=( Ref_<const Impl> ) -> Ref_<Impl> = delete;
public:
auto size() const
-> Point
{
auto const api_size = api_info().dwSize;
return Point{ api_size.x, api_size.y };
}
~Impl()
{} // TODO:
Impl( const Point size )
: text_buffer_( impl::winapi::CreateConsoleScreenBuffer(
impl::winapi::flag_GENERIC_READ | impl::winapi::flag_GENERIC_WRITE,
0, // No sharing
nullptr, // Default security.
impl::winapi::flag_CONSOLE_TEXTMODE_BUFFER, // The allowed value.
nullptr // Reserved.
) )
{
hopefully( text_buffer_ != impl::winapi::invalid_handle_value )
|| fail( "console::Display: CreateConsoleScreenBuffer failed" );
}
};
auto Display::Impl::api_info() const
-> Ref_<const Api_info>
{
if( not info_.is_valid_ )
{
impl::winapi::GetConsoleScreenBufferInfo( text_buffer_, &info_.api_info_ )
|| fail( "GetConsoleScreenBufferInfo failed" );
info_.is_valid_ = true;
}
return info_.api_info_;
}
void Display::Impl::invalidate_api_info()
{ info_.is_valid_ = false; }
After a bit of digging, I think this is what you are looking for:
§9.3 [class.mfct]:
So (as far as I understand the standard), your code is valid when you declare the member function before your nested class.
这篇关于不完整类的成员函数作为好友=正式有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!