小编xml*_*lmx的帖子

为什么ostream_iterator需要显式声明要输出的对象类型?

在当前的C++中,类ostream_iterator的设计如下:

// excerpted from the standard C++

template<class T, ...>
class ostream_iterator
{
public:
    ostream_iterator(ostream_type&);
    ...

    ostream_iterator<T,...>& operator =(const T&);
    ...
};
Run Code Online (Sandbox Code Playgroud)

对我来说,这种设计并不是最理想的.因为用户在声明ostream_iterator时必须指定类型T:ostream_iterator<int> oi(cout);事实上,cout可以将任何类型的对象作为其参数,而不是仅使用一种类型.这是一个明显的限制.

// Below is my own version

// doesn't need any template parameter here
class ostream_iterator
{
public:
    ostream_iterator(ostream_type&);
    ...

    // define a template member function which can take any type of argument and output it
    template<class T> 
    ostream_iterator<T,...>& operator =(const T&);
    ...
};
Run Code Online (Sandbox Code Playgroud)

现在,我们可以使用它如下:

ostream_iterator oi(cout);
Run Code Online (Sandbox Code Playgroud)

我认为它比它更通用,更优雅

ostream_iterator<int> oi(cout);
Run Code Online (Sandbox Code Playgroud)

我对吗?

c++ stream

10
推荐指数
1
解决办法
1341
查看次数

如何检查模板参数是否是迭代器类型?

template<class T>
struct is_iterator
{
    static const bool value = ??? // What to write ???
};

int main()
{
    assert(false == is_iterator<int>::value);
    assert(true == is_iterator<vector<int>::iterator>::value);
    assert(true == is_iterator<list<int>::iterator>::value);
    assert(true == is_iterator<string::iterator>::value);
    assert(true == is_iterator<char*>::value); // a raw pointer is also an iterator
}
Run Code Online (Sandbox Code Playgroud)

问题是:如何使五个断言语句通过?

c++ templates sfinae

10
推荐指数
2
解决办法
3507
查看次数

为什么这段代码会导致VC++编译器崩溃?

我正在使用以下编译器:

Microsoft Visual C++ 2010

以下代码在编译时崩溃:

template<class T_> 
void crasher(T_ a, decltype(*a)* dummy = 0){}

int main()
{
    crasher(0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

decltype(*a)*用于实施T_是一个指针样型-如char*,int*,和shared_ptr<int>.

为什么会崩溃?这是一个已知的错误?

c++ visual-studio-2010 c++11

10
推荐指数
1
解决办法
492
查看次数

为什么这样的结构包含两个只包含一个元素的数组字段?

请注意:这个问题不是(struct中的一个元素数组)的重复

以下代码摘自Linux内核源代码(版本:3.14)

struct files_struct
{
    atomic_t count;
    struct fdtable __rcu *fdt;
    struct fdtable fdtab;

    spinlock_t file_lock ____cacheline_aligned_in_smp;
    int next_fd;
    unsigned long close_on_exec_init[1];
    unsigned long open_fds_init[1];
    struct file __rcu * fd_array[NR_OPEN_DEFAULT];
};
Run Code Online (Sandbox Code Playgroud)

我只是想知道为什么close_on_exec_initopen_fds_init被定义为包含一个元素的数组,而不是仅仅定义为unsigned long close_on_exec_init;unsigned long open_fds_init;.

c linux arrays struct idioms

10
推荐指数
2
解决办法
364
查看次数

为什么Java泛型不允许对泛型类型进行类型转换?

public class Main {

    public static <T> void foo(T[] bar) {
        double d = (double) bar[0]; // Error : incompatible types
    }

    public static void main(String[] args) {
        int[] int_buf = new int[8];
        foo(int_buf);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题在代码中指出.

为什么Java泛型不允许对泛型类型进行类型转换?

java generics type-conversion

10
推荐指数
2
解决办法
626
查看次数

decltype可以声明一个r值吗?

// Compiled by Visual Studio 2012

struct A
{
    bool operator ==(const A& other) const
    {
        for (decltype(this->n) i = 0; i < n; ++i) // OK
        {}

        return true;
    }

protected:
    size_t n;
};

struct B : public A
{
    bool operator ==(const B& other) const
    {
        for (decltype(this->n) i = 0; i < n; ++i) // error C2105: '++' needs l-value
        {}

        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)

这是VC++ 2012的错误吗?

c++ decltype visual-c++ c++11 visual-studio-2012

9
推荐指数
1
解决办法
182
查看次数

const临时从模板类型和为什么使用std :: add_const?

以下代码摘自cppreference.com.

#include <iostream>
#include <type_traits>

struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
};

template <class T>
void call_m()
{
    T().m();
}

int main()
{
    call_m<foo>();
    call_m<std::add_const<foo>::type>();
}
Run Code Online (Sandbox Code Playgroud)

但是,使用VC++ 2012年11月的CTP编译时,输出为

非CV

非CV

而不是预期的:

非CV

常量

此外,以下两个陈述之间的区别是什么:

call_m<const foo>();

call_m<std::add_const<foo>::type>();

c++ overloading type-traits visual-c++ c++11

9
推荐指数
1
解决办法
472
查看次数

这是gcc的错误吗?

#include <codecvt>
#include <string>
#include <locale>

std::string to_gbk(const std::wstring& u16_str)
{
        using Facet = std::codecvt_byname<wchar_t, char, std::mbstate_t>;
        std::wstring_convert
                <std::codecvt<wchar_t, char, std::mbstate_t>>
                wstr_2_gbk(new Facet("zh_CN.GBK"));

        return wstr_2_gbk.to_bytes(u16_str);
}

int main()
{
        to_gbk(L"");
}
Run Code Online (Sandbox Code Playgroud)

clang和vc ++都可以,但是gcc 6.2输出:

[root@localhost ~]# g++ main.cpp 
In file included from /usr/include/c++/6.2.1/bits/locale_conv.h:41:0,
                 from /usr/include/c++/6.2.1/locale:43,
                 from main.cpp:3: /usr/include/c++/6.2.1/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = std::codecvt<wchar_t, char, __mbstate_t>]’:
/usr/include/c++/6.2.1/bits/unique_ptr.h:236:17:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = std::codecvt<wchar_t, char, __mbstate_t>; _Dp = std::default_delete<std::codecvt<wchar_t, char, __mbstate_t> >]’
/usr/include/c++/6.2.1/bits/locale_conv.h:218:7:   required from …
Run Code Online (Sandbox Code Playgroud)

c++ gcc compiler-bug

9
推荐指数
1
解决办法
961
查看次数

为什么带有初始化程序的 C++17 if 语句不能按预期工作?

struct A
{
    auto g1()
    {
        return true;
    }

    void f()
    {
        if (auto b = g1(); b) // ok
        {
            return;
        }

        if (auto b = g2(); b) // error: use of 'auto A::g2()' before deduction of 'auto'
        {
            return;
        }
    }    
    
    auto g2()
    {
        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)

为什么带有初始化程序的 C++17 if 语句不能按预期工作?

c++ language-lawyer auto type-deduction c++17

9
推荐指数
1
解决办法
481
查看次数

如何将函数模板作为模板参数传递?

#include <iostream>

template<typename... Args>
void print(Args const&... args)
{
    (std::cout << ... << args);
}

int main()
{
    std::cout << 1 << 2 << 3 << std::endl; // ok
    print(1, 2, 3);                        // ok
    print(1, 2, 3, std::endl);             // error! How to make it work?
}
Run Code Online (Sandbox Code Playgroud)

在线演示

如何将函数模板作为模板参数传递?

c++ templates overloading overload-resolution c++17

9
推荐指数
3
解决办法
155
查看次数