为什么没有必要的资格? [英] Why is no qualification necessary?

查看:136
本文介绍了为什么没有必要的资格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我只是发布完整的程序,尽管它有额外的东西,有问题的代码是死代码…

OK, I'll just post the complete program even though it has extraneous stuff and the code in question is the dead code…

#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 ?或者,同样的问题,为什么我需要在构造函数初始化列表中使用 std :: 限定 ifstream

This compiles fine with MSVC and g++. But in the InFStream class, why do I not need to qualify ios_base? Or, same question really, why do I need to use std:: qualification of ifstream in the constructor initializer list?

推荐答案

有些想法为什么你必须指定 std :: ifstream 在构造函数的初始化器。
我认为 typedef 是罪魁祸首 - ifstream 定义为 typedef basic_ifstream< char,char_traits< char> > ifstream; )。如果您将构造函数更改为

Some thoughts about why you have to specify std::ifstream in constructor's initializer . I think typedef is the culprit - ifstream is defined as typedef basic_ifstream<char, char_traits<char> > ifstream;). If you change your constructor to

 explicit InFStream(
    char const*         filename,
    ios_base::openmode  mode =  ios_base::in | ios_base::out

    ):
    basic_ifstream<char,std::char_traits<char>>( filename, mode ){}

您也不必指定 std :: basic_ifstream 。我找不到为什么 typedef 这样工作的细节,但问题是可重复的。例如,

you also don't have to specify std::basic_ifstream. I cannot find details about why typedef works this way, but the problem is reproducible. For instance,

namespace test1
{
class A {

public :
    static const int cn = 1;

    virtual ~A();
    A(int t): x(t){};
    int x;
};

class B:public A
{
public:
    B(int t) : A(t){};
};
typedef B XX;  
};  
class C:public test1::XX
{
  int aaa;
    public:
explicit  C(int x) :XX(x) // error  
explicit  C(int x) :test1::XX(x) // ok
explicit  C(int x) :B(x) // also ok
{       
    aaa = A::cn;
};
};

这篇关于为什么没有必要的资格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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