我们std::pair<T1, T2>以此为例.它有以下两个构造函数:
constexpr pair( const T1& x, const T2& y ); // #1
template< class U1, class U2 > constexpr pair( U1&& x, U2&& y ); // #2
Run Code Online (Sandbox Code Playgroud)
似乎#2可以处理#1可以处理的所有情况(没有更差的性能),除了参数是list-initializer的情况.例如,
std::pair<int, int> p({0}, {0}); // ill-formed without #1
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
如果#1仅供列表初始化参数,因为x并y最终结合到从列表初始化初始化临时对象,为什么不使用constexpr pair( T1&& x, T2&& y );呢?
否则,#1的实际意图是什么?
c++ constructor rvalue-reference move-semantics perfect-forwarding
[temp.deduct.type]第8段列出了所有推导出的上下文,但它似乎没有包括where 类引用类模板并引用模板模板参数.这是推断的背景吗?template-name<TT>template-nameTT
如果是,为什么?
如果没有,请考虑以下代码:
template<template<typename> class U, template<typename> class V>
struct foo {};
template<template<typename> class U>
struct foo<U, U> {};
int main() {}
Run Code Online (Sandbox Code Playgroud)
此代码编译锵7.0.0下和GCC 8.0.1,这意味着编译器考虑部分特比主模板,这意味着更加专业化U和V在主模板被成功地推导出对foo<U, U>.这是编译器错误吗?
考虑以下功能:
template <size_t S1, size_t S2>
auto concatenate(std::array<uint8_t, S1> &data1, std::array<uint8_t, S2> &data2) {
std::array<uint8_t, data1.size() + data2.size()> result;
auto iter = std::copy(data1.begin(), data1.end(), result.begin());
std::copy(data2.begin(), data2.end(), iter);
return result;
}
int main()
{
std::array<uint8_t, 1> data1{ 0x00 };
std::array<uint8_t, 1> data2{ 0xFF };
auto result = concatenate(data1, data2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当使用clang 6.0编译时,使用-std = c ++ 17,此函数无法编译,因为数组上的size成员函数不是constexpr,因为它是一个引用.错误消息是这样的:
错误:非类型模板参数不是常量表达式
当参数不是引用时,代码按预期工作.
我想知道为什么会这样,因为size()实际上返回一个模板参数,它几乎不再是const.参数是否是参考不应该有所作为.
我知道我当然可以使用S1和S2模板参数,该功能仅仅是问题的简短说明.
标准中有什么吗?我很惊讶地发现了编译错误.
以下示例来自cppreference.com 的std::aligned_storage 页面:
#include <iostream>
#include <type_traits>
#include <string>
template<class T, std::size_t N>
class static_vector
{
// properly aligned uninitialized storage for N T's
typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
std::size_t m_size = 0;
public:
// Create an object in aligned storage
template<typename ...Args> void emplace_back(Args&&... args)
{
if( m_size >= N ) // possible error handling
throw std::bad_alloc{};
new(data+m_size) T(std::forward<Args>(args)...);
++m_size;
}
// Access an object in aligned storage
const T& operator[](std::size_t pos) const
{
return *reinterpret_cast<const T*>(data+pos);
} …Run Code Online (Sandbox Code Playgroud) 我对[dcl.array]/1感到困惑:
在TD的声明中,D表格
D1 [ constant-expression opt ] attribute-specifier-seq opt
并且声明T D1中的标识符的类型是" derived-declarator-type-list T",则D的标识符的类型是数组类型; ...
考虑声明:
int (*p)[42];
Run Code Online (Sandbox Code Playgroud)
该声明满足上述语法(并且不满足前面段落中描述的语法),因此本段应该适用,因此我们得出结论,类型p是数组类型.但是,我们知道该类型的p就是pointer to array of 42 int,这是一个指针类型.
我错过了什么吗?或者pointer to array of 42 int确实是阵列类型?
这个问题的动机是这个。
考虑以下代码:
struct B {};
struct S {
B b; // #1
S() = default;
template <typename ...dummy> // #2
constexpr S(const S&) {}
template <typename ...dummy> // #3
constexpr S(S &other)
: S(const_cast<const S&>(other)) // #4
{}
};
S s;
constexpr S f() {return s;}
int main() {
constexpr auto x = f();
}
Run Code Online (Sandbox Code Playgroud)
GCC 成功编译了这段代码,但 Clang 拒绝了它(Godbolt.org 上的示例)。Clang 产生的错误信息是
<source>:21:20: error: constexpr variable 'x' must be initialized by a constant expression
constexpr auto x …Run Code Online (Sandbox Code Playgroud) c++ templates copy-constructor language-lawyer constant-expression
目的是从文件中读取域列表,并从头开始执行查找以确认可访问性和解析度。
这是我写的:
#!/usr/bin/python
import os
import socket
f = open('file1.lst', 'r')
s = f.readlines()
for i in s:
print i
socket.gethostbyname(i.strip())
f.close()
Run Code Online (Sandbox Code Playgroud)
socket.gethostbyname() 行引发异常。
#include <cmath>
double log(double) {return 1.0;}
int main() {
log(1.0);
}
Run Code Online (Sandbox Code Playgroud)
假设函数log()in<cmath>是在全局命名空间中声明的(这实际上是未指定的,我们只是假设),那么它引用的函数与log()我们定义的函数相同。
那么这段代码是否违反了一个定义规则(参见这里,因为不需要诊断,这段代码可能会在某些编译器中编译,我们无法断言它是否正确)?
注意:经过最近的编辑,这不是以下内容的重复:C++ 中的一个定义规则究竟是什么?
c++ one-definition-rule c++-standard-library language-lawyer c-standard-library
我正在尝试制作一个正则表达式来识别评论。它必须//以新行或*)模式开始和结束。
目前,我设法得到了这个(\/\/)([^\n\r]+),但我没有成功添加*)模式。
有小费吗?
有没有办法在pytube中添加进度条?我不知道如何使用以下方法:
pytube.Stream().on_progress(chunk, file_handler, bytes_remaining)
Run Code Online (Sandbox Code Playgroud)
我的代码:
from pytube import YouTube
# from pytube import Stream
from general import append_to_file
def downloader(video_link, down_dir=None):
try:
tube = YouTube(video_link)
title = tube.title
print("Now downloading, " + str(title))
video = tube.streams.filter(progressive=True, file_extension='mp4').first()
print('FileSize : ' + str(round(video.filesize/(1024*1024))) + 'MB')
# print(tube.streams.filter(progressive=True, file_extension='mp4').first())
# Stream(video).on_progress()
if down_dir is not None:
video.download(down_dir)
else:
video.download()
print("Download complete, " + str(title))
caption = tube.captions.get_by_language_code('en')
if caption is not None:
subtitle = caption.generate_srt_captions()
open(title + '.srt', 'w').write(subtitle)
except Exception as …Run Code Online (Sandbox Code Playgroud) c++ ×8
python ×2
templates ×2
arrays ×1
constructor ×1
declaration ×1
dns ×1
file-io ×1
pytube ×1
regex ×1
sockets ×1
template-argument-deduction ×1
youtube ×1