好的,我只是发布完整的程序,即使它有无关的东西,并且所讨论的代码是死代码。为什么没有必要的资格?
#include <iostream>
#include <fstream>
namespace detail {
// Solution by Johannes Schaub alias litb
// http://groups.google.com/group/comp.std.c++/browse_thread/thread/b567617bfccabcad
template<int> struct D {};
typedef char yes[1];
typedef char no[2];
template< class T, class U >
yes& f(int, D< sizeof T(*(U*)0) >* = 0);
template< class T, class U >
no& f(...);
template< class To, class From >
struct IsExplicitlyConvertible
{
enum{ yes = (sizeof detail::f< To, From >(0) == sizeof(detail::yes)) };
};
bool const streamsSupportWindows =
IsExplicitlyConvertible< std::ofstream, wchar_t const* >::yes;
}
class InFStream
: public std::ifstream
{
public:
InFStream() {}
explicit InFStream(
char const* filename,
ios_base::openmode mode = ios_base::in | ios_base::out
)
: std::ifstream(filename, mode)
{}
};
int main()
{
using namespace std;
cout << (detail::streamsSupportWindows
? "Windows-enabled"
: "Ach, no Windows support"
) << endl;
}
这个编译罚款与MSVC和g ++。但在InFStream
班,为什么我不需要限制ios_base
?或者,同样的问题,为什么我需要在构造函数初始化列表中使用ifstream
的std::
资格?
因为'ifstream'是在std命名空间中吗?你为什么认为你不需要使用它? –
@VJo:'ios_base'也在'std'命名空间中。至于为什么我认为我提到的编译器编译这些代码很好,那是因为他们这样做。嘿。 –
可能是因为您的InFStream类继承自std :: ifstream,而std :: ifstream又继承自std :: ios_base。因此,所有的ios_base枚举都是通过继承的类的成员,并且不需要完全限定。 – Praetorian