std::tie(a, b) = std::minmax(a, b);
Run Code Online (Sandbox Code Playgroud)
我认为这是直观的代码。干净易懂。太糟糕了,它作为的std::minmax
模板无法正常工作const&
。因此,如果在内部交换值,则std::pair<const&, const&>
一个分配将覆盖另一个值:
auto[a, b] = std::make_pair(7, 5);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << '\n';
Run Code Online (Sandbox Code Playgroud)
a:5,b:5
此处的预期输出为a: 5, b: 7
。
我认为这很重要,因为实现转换功能以将功能应用于某些范围需要直观的lambda声明。例如:
std::vector<int> v{ 0, 1, 0, 2, 0 };
std::vector<int> u{ 1, 0, 1, 0, 1 };
perform(v.begin(), v.end(), u.begin(), [](auto& a, auto& b){
std::tie(a, b) = std::minmax(a, b);
});
//v would be == {0, 0, …
Run Code Online (Sandbox Code Playgroud) 在发布模式下,我的谷歌登录无法正常工作.但它在调试模式下工作正常.我在这里得到了类似的问题.但我没有得到完美的灵魂.我该handleSignInResult
怎么办????
我有一个build
具有标志模板的类型,并且根据活动标志位,它继承自这些类型。这使我能够从具有大量配置的许多子类“构建”类:
#include <type_traits>
#include <cstdint>
struct A { void a() {} };
struct B { void b() {} };
struct C { void c() {} };
struct D { void d() {} };
constexpr std::uint8_t FLAG_BIT_A = 0b1 << 0;
constexpr std::uint8_t FLAG_BIT_B = 0b1 << 1;
constexpr std::uint8_t FLAG_BIT_C = 0b1 << 2;
constexpr std::uint8_t FLAG_BIT_D = 0b1 << 3;
struct empty {};
template<std::uint8_t flags>
using flag_a_type = std::conditional_t<(flags & FLAG_BIT_A), A, empty>;
template<std::uint8_t flags>
using flag_b_type …
Run Code Online (Sandbox Code Playgroud) 当我发现以下可编译gcc
但不编译的代码时,我正在尝试在折叠表达式中使用任意函数clang
。
enum Enum {
A = 3,
B = 8,
C = 5
};
namespace EnumMax {
constexpr Enum operator>>=(const Enum left, const Enum right) {
return left < right ? right : left;
}
}
template<Enum ... enums>
constexpr Enum max() {
using EnumMax::operator>>=;
return (enums >>= ...);
}
constexpr Enum max_v = max<A, B, C>();
Run Code Online (Sandbox Code Playgroud)
似乎clang
不考虑重载运算符,而是尝试>>=
在fold表达式中使用正则运算符。
但是,如果改写了fold表达式,clang
则考虑重载运算符,并且可以正常编译:
constexpr Enum maxExplicit() {
using EnumMax::operator>>=;
return (A >>= (B …
Run Code Online (Sandbox Code Playgroud) 使用列表初始化(如int x{ 5 };
)构造变量时,标准 §8.5.4表示:
如果需要缩小转换[…]来转换任何参数,则程序格式错误。 (7)缩小转换是隐式转换- (7.4)从整数类型或无作用域枚举类型到不能表示原始类型的所有值的整数类型,除非源是一个常数表达式,其值在整数提升后将适合目标类型。
那么为什么要编译呢?
char c{ 'A' };
char x{ c + c };
Run Code Online (Sandbox Code Playgroud)
作为提醒,c + c
产生一个int
static_assert(std::is_same_v<decltype(c + c), int>, "");
Run Code Online (Sandbox Code Playgroud)
因此,编译器应该抱怨转换变窄,这肯定不是常量表达式。
有趣的是,声明x
为unsigned char
正确编译失败:
char c{ 'A' };
unsigned char x{ c + c };
Run Code Online (Sandbox Code Playgroud)
C2397从'int'到'unsigned char'的转换需要缩小的转换
就像介绍临时文件一样:
char c{ 'A' };
int sum{ c + c };
char x{ sum }; //C2397 conversion from 'int' to 'char' requires [...]
Run Code Online (Sandbox Code Playgroud)
那么为什么要编译第一个版本呢?我正在使用Visual Studio …
我有两个变量,我想分别处理较大和较小的一个。
我的方法:
a = 1;
b = 2;
if (a >= b){
int c = a;
int d = b;
}
else{
int c = b;
int d = a;
}
Run Code Online (Sandbox Code Playgroud)
我获得的未使用的变量的错误,当我尝试使用c
和d
以后,它说
c
无法解决
有办法解决吗?
对于实现条件类型,我非常喜欢std::conditional_t
它,因为它使代码简短且可读性强:
template<std::size_t N>
using bit_type =
std::conditional_t<N == std::size_t{ 8 }, std::uint8_t,
std::conditional_t<N == std::size_t{ 16 }, std::uint16_t,
std::conditional_t<N == std::size_t{ 32 }, std::uint32_t,
std::conditional_t<N == std::size_t{ 64 }, std::uint64_t, void>>>>;
Run Code Online (Sandbox Code Playgroud)
使用它非常直观:
bit_type<8u> a; // == std::uint8_t
bit_type<16u> b; // == std::uint16_t
bit_type<32u> c; // == std::uint32_t
bit_type<64u> d; // == std::uint64_t
Run Code Online (Sandbox Code Playgroud)
但是,由于这是纯条件类型void
,因此在这种情况下必须有默认类型- 。因此,如果N
还有其他值,则该类型将产生:
bit_type<500u> f; // == void
Run Code Online (Sandbox Code Playgroud)
现在,它不会编译,但是yielding类型仍然有效。
意味着您可以说bit_type<500u>* f;
并且将拥有一个有效的程序!
那么,当达到条件类型的失败情况时,有没有一种好的方法让编译失败?
一个想法立即将取代过去std::conditional_t
用std::enable_if_t
:
template<std::size_t N>
using bit_type …
Run Code Online (Sandbox Code Playgroud) c++ templates template-meta-programming c++11 conditional-types
为什么我不能为该Geometry
对象调用适当的构造函数?
class Geometry {
private:
float fRadius;
int iSegments;
float fWidth;
float fLenght;
std::string stdstrType;
bool bValid;
public:
Geometry() {
// Set data Elements
qDebug() << "Constructor 1 is called";
}
Geometry(float Radius, int Segments, float Width, float Length,
std::string strType, bool bValue) {
// Set data Elements
qDebug() << "Constructor 2 is called";
}
Geometry(const Geometry & g) {
// Set data Elements
qDebug() << "Constructor 3 is called";
}
}
Run Code Online (Sandbox Code Playgroud)
我将此类用作另一个类中的数据变量。
class Container {
private:
std::string …
Run Code Online (Sandbox Code Playgroud) 我有以下几点:
Ionic:
ionic (Ionic CLI) : 4.7.1 (/usr/local/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.1.2
@angular-devkit/build-angular : 0.13.7
@angular-devkit/schematics : 7.2.4
@angular/cli : 7.3.7
@ionic/angular-toolkit : 1.4.1
Cordova:
cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
Cordova Platforms : android 7.1.4, browser 5.0.4, ios 4.5.5
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 3.1.2, (and 10 other plugins)
System:
Android SDK Tools : 26.1.1 (/Users/pds/Library/Android/sdk)
NodeJS : v11.6.0 (/usr/local/bin/node)
npm : 6.5.0-next.0
OS : macOS Mojave
Xcode : Xcode 10.2 Build version 10E125
Run Code Online (Sandbox Code Playgroud)
根据此文档:https://ionicframework.com/docs/api/item#detail-arrows,为了不在 …
一切都很好,直到一次更新更改了格式设置,并且我不知道如何反转它。
每当我选择 C++ 代码并尝试用左大括号替换文本时,它只会包围所选文本,而不是用左大括号替换它。
一段时间后,这会变得非常令人沮丧,例如,在使用选择时尝试用符号替换<
less 符号>
现在是不可能的。我怎样才能禁用这种行为?
我正在使用 Visual Studio Community 2022 版本 17.4.0
c++ ×7
c++11 ×3
c++17 ×2
algorithm ×1
android ×1
autocomplete ×1
clang ×1
formatting ×1
inheritance ×1
ionic4 ×1
ios ×1
reference ×1
stl ×1
templates ×1
visual-c++ ×1