在当前的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)
我对吗?
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)
问题是:如何使五个断言语句通过?
我正在使用以下编译器:
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>
.
为什么会崩溃?这是一个已知的错误?
请注意:这个问题不是(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_init
和open_fds_init
被定义为包含一个元素的数组,而不是仅仅定义为unsigned long close_on_exec_init;
和unsigned long open_fds_init;
.
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泛型不允许对泛型类型进行类型转换?
// 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的错误吗?
以下代码摘自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>();
#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) 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 语句不能按预期工作?
#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++ ×8
c++11 ×3
c++17 ×2
overloading ×2
templates ×2
visual-c++ ×2
arrays ×1
auto ×1
c ×1
compiler-bug ×1
decltype ×1
gcc ×1
generics ×1
idioms ×1
java ×1
linux ×1
sfinae ×1
stream ×1
struct ×1
type-traits ×1