考虑一下这段代码:
struct Base
{
    int x;
};
struct Bar : Base
{
    int y;
};
struct Foo : Base
{
    int z;
};
Bar* bar = new Bar;
Foo* foo = new Foo;
Base* returnBase()
{
    Base* obj = !bar ? foo : bar;
    return obj;
}
int main() {
    returnBase();
    return 0;
}
这在Clang或GCC下不起作用,给我:
错误:不同指针类型'Foo*'和'Bar*'之间的条件表达式缺少一个强制转换Base*obj =!bar?foo:bar;
这意味着要编译我必须将代码更改为:
Base* obj = !bar ? static_cast<Base*>(foo) : bar;
由于隐式转换为Base*存在,是什么阻止编译器这样做?
换句话说,为什么没有Base* obj = foo;演员而是使用?:操作员的工作呢?是因为我不清楚我想使用这Base部分吗?
c++ ternary-operator language-lawyer implicit-conversion c++17
有人可以给我一个解释,为什么我得到两个不同的数字,分别是.在图14和15中,作为以下代码的输出?
#include <stdio.h>  
int main()
{
    double Vmax = 2.9; 
    double Vmin = 1.4; 
    double step = 0.1; 
    double a =(Vmax-Vmin)/step;
    int b = (Vmax-Vmin)/step;
    int c = a;
    printf("%d  %d",b,c);  // 14 15, why?
    return 0;
}
我希望在两种情况下都能获得15分,但似乎我缺少一些语言的基础知识.
我不确定它是否相关,但我在CodeBlocks中进行测试.但是,如果我在某些在线编译器中键入相同的代码行(例如这个),我会得到两个打印变量的答案为15.
有人可以解释一下这段代码中发生了什么:
这里的例子: https : //ideone.com/1cFb4N
#include <iostream>
using namespace std;
class toto
{
public:
    bool b;
    toto(bool x)
    {
        cout<< "constructor bool:" << (x ? "true": "false")<<endl;
        b = x;
    }
    ~toto() {}
};
int main()
{
    toto t = new toto(0);
    cout << "t.b is " << (t.b ? "true": "false")<<endl;
    t = new toto(false);
    cout << "t.b is " << (t.b ? "true": "false")<<endl;
    return 0;
}
输出:
constructor bool:false
constructor bool:true
t.b is true
constructor bool:false
constructor …看来 GCC 和 Clang 对有符号整数和无符号整数之间的加法的解释不同,具体取决于它们的大小。为什么会这样?所有编译器和平台上的转换是否一致?
举个例子:
#include <cstdint>
#include <iostream>
int main()
{
    std::cout <<"16 bit uint 2 - int 3 = "<<uint16_t(2)+int16_t(-3)<<std::endl;
    std::cout <<"32 bit uint 2 - int 3 = "<<uint32_t(2)+int32_t(-3)<<std::endl;
    return 0;
}
结果:
$ ./out.exe   
16 bit uint 2 - int 3 = -1
32 bit uint 2 - int 3 = 4294967295
在这两种情况下,我们都得到 -1,但其中一个被解释为无符号整数并下溢。我本以为两者都会以相同的方式进行转换。
那么,为什么编译器对它们的转换如此不同,这能保证一致吗?我用 g++ 11.1.0、clang 12.0 对此进行了测试。以及 Arch Linux 和 Debian 上的 g++ 11.2.0,得到相同的结果。
鉴于一些字典
Dictionary<string, string> GroupNames = new Dictionary<string, string>();
Dictionary<string, string> AddedGroupNames = new Dictionary<string, string>();
我无法将它们合并为一个:
GroupNames = GroupNames.Concat(AddedGroupNames);
因为"类型不能被隐式转换".我相信(我的代码证明我是真的)他们的类型是一样的 - 我可以忽略什么?
我正在使用void*在某些功能中接受的 API 。我经常不小心将错误的指针类型传递给函数,当然它编译得很好,但在运行时不起作用。
有没有办法禁用void*指向某个类的指针的隐式转换?
我创建了几个用于处理议程约会的接口和泛型类:
interface IAppointment<T> where T : IAppointmentProperties
{
    T Properties { get; set; }
}
interface IAppointmentEntry<T> where T : IAppointment<IAppointmentProperties>
{
    DateTime Date { get; set; }
    T Appointment { get; set; }
}
interface IAppointmentProperties 
{
    string Description { get; set; }
}
class Appointment<T> : IAppointment<T> where T : IAppointmentProperties
{
    public T Properties { get; set; }
}
class AppointmentEntry<T> : IAppointmentEntry<T> where T : IAppointment<IAppointmentProperties>
{
    public DateTime Date { get; set; }
    public T …我看到整个C++标准中许多地方使用的术语"左值到右值转换".据我所知,这种转换通常是隐含的.
标准中的一个意外(对我来说)特征是他们决定将左值作为转换处理.如果他们说glvalue总是可以接受而不是prvalue怎么办?这句话实际上会有不同的含义吗?例如,我们读到lvalues和xvalues是glvalues的例子.我们没有读到lvalues和xvalues可以转换为glvalues.意义上有区别吗?
在我第一次遇到这个术语之前,我曾经或多或少地对lvalues和rvalues进行了如下建模:"lvalues 总是能够充当rvalues,但另外还可以出现在左侧=和右侧&".
对我来说,这是一个直观的行为,如果我有一个变量名,那么我可以将该名称放在我想放置文字的地方.此模型似乎与标准中使用的左值到右值隐式转换术语一致,只要保证发生此隐式转换即可.
但是,因为他们使用这个术语,我开始想知道在某些情况下是否可能无法进行隐式左值到右值转换.也就是说,也许我的心理模型在这里是错误的.以下是该标准的相关部分:(感谢评论者).
每当glvalue出现在期望prvalue的上下文中时,glvalue就会转换为prvalue; 见4.1,4.2和4.3.[注意:尝试将rvalue引用绑定到左值不是这样的上下文; 见8.5.3 .-结束说明]
我理解他们在说明中描述的内容如下:
int x = 1;
int && y = x; //in this declaration context, x won't bind to y.
// but the literal 1 would have bound, so this is one context where the implicit 
// lvalue to rvalue conversion did not happen.  
// The expression on right is an lvalue. if it had been a prvalue, it would have bound.
// Therefore, the lvalue …我似乎找到了Clang和GCC不同意的东西.这是代码:
int main() {
  if constexpr (2) {}
}
这成功编译了GCC 7.4.0,但它与Clang 7.0.0失败并出现此错误消息:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
      [-Wc++11-narrowing]
  if constexpr (2) {}
                ^
1 error generated.
cppreference似乎没有提到"缩小",所以这看起来像一个Clang bug,但我不完全确定.如果这是任何一个编译器的错误,是否已报告?
如何避免对非构造函数进行隐式转换?
我有一个函数,它接受一个整数作为参数,
但该函数也将采用字符,bools和long.
我相信它是通过隐式地施放它来实现的.
如何避免这种情况,以便函数只接受匹配类型的参数,否则将拒绝编译?
有一个关键字"显式"但它不适用于非构造函数.:\ 
我该怎么办?
以下程序编译,虽然我不喜欢:
#include <cstdlib>
//the function signature requires an int
void function(int i);
int main(){
    int i{5};
    function(i); //<- this is acceptable
    char c{'a'};
    function(c); //<- I would NOT like this to compile
    return EXIT_SUCCESS;
}
void function(int i){return;}
*请务必指出任何滥用术语和假设的行为