在将指针传递给需要常量引用的函数时,我看到了一些奇怪的行为。显然,C++ 希望我在将指针传递给期望引用的函数之前取消引用指针。我正在寻找错误或警告,但除非满足某个对我没有任何意义的条件,否则我不会收到错误或警告。
指针和引用的类型是 C++ 17 标准变体(如下)。如果我的 C++17 变体在模板中包含布尔值(如下所示),那么 GCC8.3 可以很好地编译代码。但我有意想不到的运行时结果。
如果我从标准变体模板中删除布尔值,则代码不会按我预期的那样编译。为什么有区别?
#include <iostream>
#include <variant>
#include <stdint.h>
typedef std::variant <
std::monostate,
int8_t,
int16_t,
int32_t,
int64_t,
uint8_t,
uint16_t,
uint32_t,
uint64_t,
float,
double
,bool //If bool is in the variant, the compiler compiles the code without error and understandably has UB. Why isn't the lack of de-reference bug in DoThing not caught when that is the case?
> StdVARIANT;
//if 'bool' is commented out of the variant the error (expected) is:
//invalid initialization of reference of type ‘const StdVARIANT&’
static size_t GetIndexOfVariant(const StdVARIANT& RefToVariant);
static size_t DoThing(StdVARIANT* PtrToVariant);
static size_t DoThing(StdVARIANT* PtrToVariant){
return GetIndexOfVariant(PtrToVariant); //this is a bug-PtrToVariant should be de referenced!
}
static size_t GetIndexOfVariant(const StdVARIANT& RefToVariant){
size_t Index = RefToVariant.index();
return Index;
}
int main()
{
StdVARIANT* myvar = new StdVARIANT();
*myvar = (int32_t)1;
std::cout<<"long: "<<std::get<int32_t>(*myvar)<<" "<<sizeof(std::get<int32_t>(*myvar))<<std::endl;
*myvar = (double)2;
std::cout<<"double: "<<std::get<double>(*myvar)<<" "<<sizeof(std::get<double>(*myvar))<<std::endl;
std::cout<< myvar->index() << " " << DoThing(myvar)<<std::endl; //when 'bool' is in the std::variant, these two calls return different results (UB)
delete myvar;
return 0;
}
编译并运行为上面的代码块:
~/gcc-test$ g++ -std=c++17 -Wall -Wextra -pedantic main.cpp
~/gcc-test$ ./a.out
long: 1 4
double: 2 8
10 11
从 StdVARIANT 中注释 'bool',并且:
~/gcc-test$ g++ -std=c++17 -Wall -Wextra -pedantic main.cpp
main.cpp: In function ‘size_t DoThing(StdVARIANT*)’:
main.cpp:27:30: error: invalid initialization of reference of type ‘const StdVARIANT&’ {aka ‘const std::variant<std::monostate, signed char, short int, int, long int, unsigned char, short unsigned int, unsigned int, long unsigned int, float, double>&’} from expression of type ‘StdVARIANT*’ {aka ‘std::variant<std::monostate, signed char, short int, int, long int, unsigned char, short unsigned int, unsigned int, long unsigned int, float, double>*’}
return GetIndexOfVariant(PtrToVariant); //this is a bug-PtrToVariant should be de referenced!
^~~~~~~~~~~~
main.cpp:23:15: note: in passing argument 1 of ‘size_t GetIndexOfVariant(const StdVARIANT&)’
static size_t GetIndexOfVariant(const StdVARIANT& RefToVariant);
^~~~~~~~~~~~~~~~~
为什么我不总是得到错误,而只是在 bool 被注释掉时?