C++标准草案N4296说
[class.temporary/5]第二个上下文是引用绑定到临时的.引用绑定的临时值或作为绑定引用的子对象的完整对象的临时值在引用的生命周期内持续存在,除了...
所以我想知道如果两个或多个引用绑定到临时引发会发生什么.它在标准中是否具体?以下代码可能是一个示例:
#include <iostream> //std::cout
#include <string> //std::string
const std::string &f() {
const std::string &s = "hello";
static const std::string &ss = s;
return ss;
}
int main() {
const std::string &rcs = f();
std::cout << rcs; //empty output
//the lifetime of the temporary is the same as that of s
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我们改变边界顺序,情况就不同了.
#include <iostream> //std::cout
#include <string> //std::string
const std::string &f() {
static const std::string &ss = "hello";
const std::string &s = ss;
return …Run Code Online (Sandbox Code Playgroud) 其实问题出在标准草案N4582中的一句话:
[basic.start.static/3] 允许实现将具有静态或线程存储持续时间的变量初始化为静态初始化,即使此类初始化不需要静态完成,前提是
— 初始化的动态版本在初始化之前不会更改任何其他静态或线程存储持续时间对象的值,并且
— 如果所有不需要静态初始化的变量都被动态初始化,则初始化的静态版本在初始化变量中产生的值与动态初始化产生的值相同。
这些话是不是意味着如果满足这两个条件,类类型的非局部变量可能会被完全静态初始化(零初始化),这样它的构造函数就不会被调用(因为动态版本,通过调用构造函数初始化,可能被静态版本取代)?
#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
考虑:
#include <type_traits>
template <typename>
struct Tag {};
template <typename T>
auto tag = Tag<T>{};
template <typename...>
struct SelectorImpl;
// 1
template <auto... xs>
struct SelectorImpl<std::integral_constant<decltype(xs), xs>...>
{};
// 2
template <typename T, Tag<T>* tag, auto... xs>
struct SelectorImpl<std::integral_constant<decltype(tag), tag>,
std::integral_constant<decltype(xs), xs>...>
{};
template <auto... params>
struct Selector
: SelectorImpl<std::integral_constant<decltype(params), params>...>
{};
int main() {
Selector<&tag<int>, 1, 2>{};
}
Run Code Online (Sandbox Code Playgroud)
gcc和clang都无法编译它,报告这些特殊化SelectorImpl是不明确的.我相信专业化#2更专业.我错了吗?难道同样的问题,因为在这里?这是一个错误吗?
假设我们有
enum class Month {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
Run Code Online (Sandbox Code Playgroud)
每个值都是一个0到11的整数。然后,我希望Month类型的变量仅保存这些枚举值。因此,这是创建变量的唯一可行方法:
Month m = Month::may;
Run Code Online (Sandbox Code Playgroud)
但是以下是该语言允许的其他一些方式:
Month m1 = Month(12345);
Month m2 = static_cast<Month>(12345);
Run Code Online (Sandbox Code Playgroud)
这有点令人失望。我如何只允许第一种方式?或者人们如何应对C ++中的穷举枚举?
我正在尝试制作一个正则表达式来识别评论。它必须//以新行或*)模式开始和结束。
目前,我设法得到了这个(\/\/)([^\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) 左移的结果可能是未定义的行为:
E1 << E2的值是E1左移E2位位置; 空位是零填充的.如果E1具有无符号类型,则结果的值为E1×2 ^ E2,比结果类型中可表示的最大值减少一个模数.否则,如果E1具有有符号类型和非负值,并且E1×2 ^ E2可在结果类型的相应无符号类型中表示,则转换为结果类型的该值是结果值; 否则,行为未定义.
右移的结果可以是实现定义的:
E1 >> E2的值是E1右移E2位位置.如果E1具有无符号类型或者E1具有有符号类型和非负值,则结果的值是E1/2 ^ E2的商的整数部分.如果E1具有带符号类型和负值,则结果值是实现定义的.
现在,我所知道的所有平台,未定义的行为/实现定义实际上在这里做了明智的事情:
所以,问题是,是否存在任何2补码平台/编译器,它们的行为不是这样的?
为什么这样问?大多数编译器不发射最佳代码(由phuclv提供的链接,之间检查出的拆卸test1和test2)为幂的2区在某些情况下(铛生成最佳代码,虽然).
我想使用std :: istream :: operator >>将数据提取为无符号类型(位于模板内,因此可以是ushort,uint等)。具体来说,我正在使用std :: stringstream解析通过std :: getline()调用从文件中提取的std :: string行。
由于我正在从文件中读取数据,因此这些提取可能由于不同的原因而失败:下溢,溢出和“错误提取”。此类情况由STL处理:
如果提取失败,则将零写入值并设置故障位。如果提取导致值太大或太小而无法容纳该值,则将写入std :: numeric_limits :: max()或std :: numeric_limits :: min()并设置故障位标志。
问题:对于无符号类型,std :: numeric_limits :: min()等于0,因此无法知道我是否正在读取不是整数的内容(在这种情况下,我正在中止程序)或只是下溢(在这种情况下,我只是钳制值并发出警告)。
如何解决此问题而不使用与我合作的无符号类型的更大和/或带符号的等效项?
从 C++20 开始,在[namespace.std]/7 中引入了定制点的概念:
除了在命名空间 std 或命名空间 std 内的命名空间中,程序可以为指定为自定义点的任何库函数模板提供重载,前提是 (a) 重载的声明依赖于至少一个用户定义的类型和 (b ) 重载满足自定义点的标准库要求。[ 注意:这允许对自定义点的(限定或非限定)调用为给定参数调用最合适的重载。— 尾注 ]
注释部分(注意强调的词“合格”)是否意味着std::f将自动调用最合适的重载fifstd::f是自定义点?
一个真实的例子是std::swap,这是一个指定的定制点。这是否意味着从 C++20 开始,我们可以std::swap(a, b)直接编写而不是using std::swap; swap(a, b);?
c++ overloading c++-standard-library argument-dependent-lookup c++20