这是代码: class A{
public:
int val;
char cval;
A():val(10),cval('a'){ }
operator char() const{ return cval; }
operator int() const{ return val; }
};
int main()
{
A a;
cout << a
我问的最后一个问题是我在试图理解另一件事时偶然发现的事情……我也无法理解(不是我的一天). 这是一个很长的问题陈述,但至少我希望这个问题可能对许多人有用,而不仅仅是我. 我的代码如下: template <typename T> class V;
template <typename T> class S;
template <typename T>
class V
{
public:
T x
如何防止这样的代码编译? #include <vector>
#include <limits>
#include <iostream>
#include <cstdint>
int main() {
std::vector<int16_t> v;
v.emplace_back(std::numeric_limits<uint64_t>::max());
std::cout << v
请考虑以下代码: void f(int x)
{
std::cout << x << std::endl;
}
int main()
{
double x = 1.5;
f(x);
return 0;
} 这编译并运行时没有任何警告(使用-Wall),因此隐藏了从double到int的危险隐式转换.如果用文字调用函数,编译器将捕获强制转换,即 f(1.5) 但这并
以下代码在C 11中成功编译: #include "json.hpp"
using json = nlohmann::json ;
using namespace std ;
int main(){
json js = "asd" ;
string s1 = js ; // <---- compiles fine
//string s2 = (string)js ; /
基本上,如果我想要这样的东西, double b = sin(2.2); 但不小心写了这样的东西, double b = sin(2.2f); 没有错误甚至是警告信息,即使这显然会导致不同的,不准确的,因此不正确的结果.通过强制编译器不执行float到double的任何隐式转换,可以防止这种类型的错误.有没有办法实现这一点,无论是通过编译开关(最好是在Visual Studio中),一些智能宏,还
使用VC 2010,给出以下内容: class Base { };
class Derived : public Base { };
template<class T> void foo(T& t); // A
void foo(Base& base); // B
Derived d;
foo(d); // ca
参见英文答案 > How to correctly use std::reference_wrappers 1个 我不明白为什么不能像这样使用std :: reference_wrapper: #include <vector>
#include <functional>
struct Foo
{
void f() {
d1 4作品,但4 d1甚至不能将4隐含地转换为GMan.为什么他们不等同? struct GMan
{
int a, b;
GMan() : a(), b() {}
GMan(int _a) : a(_a), b() {}
GMan(int _a, int _b) : a(_a), b(_b) {}
GMan operator +(const GMan
我很困惑,为什么以下代码无法编译 int foo(const float* &a) {
return 0;
}
int main() {
float* a;
foo(a);
return 0;
} 编译器给出错误: error: invalid initialization of reference of type ‘const float*&’ from exp
我猜(确定)隐式转换适用于传递非类型模板参数.例如,对于诸如std :: array< int,7>之类的表达式,应该有一个从int到std :: size_t的转换.但是,请考虑以下代码: template <bool>
void f() {
std::cout << "false\n";
}
template <>
void f<true>() {
std::cout <<
我正在做一个带有转发构造函数的瘦派生类. (与我一起,我必须使用GCC 4.7.2,它缺少继承的构造函数). 在第一次尝试时,我忘记添加了explicit关键字并且收到错误.有人可以准确地解释为什么这个特定的错误发生了吗?我无法弄清事件的顺序. #include <memory>
template<typename T>
struct shared_ptr : std::shared_ptr<T
我不明白为什么下面的代码打印结构值而不是int(这意味着转换构造函数正在转换为值而不是int). (Visual C 2012) 为什么会发生这种情况?为什么编译器完全忽略Value(int)构造函数? #include <iostream>
#include <type_info>
using namespace std;
struct Value { Value(int) { } };
我很难找到关于这个问题的信息,我可以很容易地理解,所以我要求我回顾一下我发现的内容.这只是关于转换和转换的. 在例子中我将指的是: (signed/unsigned) int bigger;
(signed/unsigned) char smaller; >截断整数. (超大化>小) >首先在MSB侧截断更大以匹配较小的大小. >第二,根据较小的类型将截断的结果转换为signed / unsign
我正在使用一个编译器的MPC56XX(嵌入式系统),一个int和long都是32位宽. 在所需的软件包中,我们对32位宽类型有以下定义: typedef signed int sint32;
typedef unsigned int uint32; 在一个新版本中,这个更改没有太多的文档: typedef signed long sint32;
typedef unsigned long
这是代码: #include <stdint.h>
unsigned char f(uint32_t RGBA)
{
return (RGBA>>24) & 0xFF;
} 当使用-Wconversion编译时,会导致“警告:转换为’unsigned char’from’uint32_t {aka unsigned int}’可能会改变其值[-Wconversion]”.如果将shift值降
我刚碰到这行代码: if( lineDirection.length2() ){...} 其中length2返回一个double.有点让我感到困惑的是0.0等于0,NULL和/或false. 这是C标准的一部分还是未定义的行为? 这是一个非常标准的行为(布尔转换) $4.12/1 – “An rvalue of arithmetic, enumeration, pointer, or pointe
参见英文答案 > sizeof() operator in if-statement 5个 这里真的发生了什么现在的输出是“False”: #include <stdio.h>
int main()
{
if (sizeof(int) > any_negative_integer)
printf("
我刚刚从C 14标准(我的重点)中读到了这个: 4.9 Floating-integral conversions [conv.fpint] 1 A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fra
使用g 3.4和4.7,我观察到以下奇怪的行为: 如果需要用户定义的转换,则函数模板不匹配,其中普通函数将是. 我在C 98标准中找不到相应的规则.是正确的,(我假设)?或者这是一个错误? template <class T>
int x(auto_ptr_ref<T> p)
{
return 1;
}
// this would match
/*
int x(auto_ptr_ref<int