header file - <typeinfo>

header file -

Type information - 类型信息

This header defines types used related to operators typeid and dynamic_cast.
这个头文件定义了与操作符 typeid and dynamic_cast 相关的类型。

The inclusion of this header is required prior to any use of the typeid operator.
在使用 typeid 运算符之前,必须包含此标头。

C++ 并没有规定 typeid 的实现标准,不同编译器可能会不一样。

1. Linux

1.1 cannot convert ‘std::shared_ptr to ‘const int*’

/*
============================================================================
Name        : yongqiang.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#include 
#include 
#include 
#include 

void output(std::string msg, const int *pint) {
    std::cout << msg << *pint << "\n";
}

int main() {
    int *pint = new int(10);
    std::shared_ptr pshared = std::make_shared(9);

    std::cout << "data type: " << typeid(pint).name() << std::endl;
    std::cout << "data type (owned pointer): " << typeid(pshared).name() << std::endl;
    std::cout << "data type (stored pointer): " << typeid(pshared.get()).name() << std::endl;
    std::cout << "data: " << *pint << "\n";
    std::cout << "data (owned pointer): " << *pshared << "\n";
    std::cout << "data (stored pointer): " << *(pshared.get()) << "\n";

    output("Naked pointer: ", pint);
    output("Shared pointer ", pshared);
    output("Shared pointer with get(): ", pshared.get());

    delete pint;

    return 0;
}

====================[ Build | yongqiang | Debug ]===============================
/home/yongqiang/software/clion-2019.1.4/bin/cmake/linux/bin/cmake --build /home/yongqiang/CLionProjects/yongqiang/cmake-build-debug --target yongqiang -- -j 6
Scanning dependencies of target yongqiang
[ 50%] Building CXX object CMakeFiles/yongqiang.dir/main.cpp.o
/home/yongqiang/CLionProjects/yongqiang/main.cpp: In function ‘int main()’:
/home/yongqiang/CLionProjects/yongqiang/main.cpp:32:38: error: cannot convert ‘std::shared_ptr’ to ‘const int*’ for argument ‘2’ to ‘void output(std::__cxx11::string, const int*)’
     output("Shared pointer ", pshared);
                                      ^
CMakeFiles/yongqiang.dir/build.make:62: recipe for target 'CMakeFiles/yongqiang.dir/main.cpp.o' failed
make[3]: *** [CMakeFiles/yongqiang.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/yongqiang.dir/all' failed
make[2]: *** [CMakeFiles/yongqiang.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/yongqiang.dir/rule' failed
make[1]: *** [CMakeFiles/yongqiang.dir/rule] Error 2
Makefile:118: recipe for target 'yongqiang' failed
make: *** [yongqiang] Error 2

1.2 stored pointer - owned pointer

/*
============================================================================
Name        : yongqiang.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#include 
#include 
#include 
#include 

void output(std::string msg, const int *pint) {
    std::cout << msg << *pint << "\n";
}

int main() {
    int *pint = new int(10);
    std::shared_ptr pshared = std::make_shared(9);

    std::cout << "data type: " << typeid(pint).name() << std::endl;
    std::cout << "data type (owned pointer): " << typeid(pshared).name() << std::endl;
    std::cout << "data type (stored pointer): " << typeid(pshared.get()).name() << std::endl;
    std::cout << "data: " << *pint << "\n";
    std::cout << "data (owned pointer): " << *pshared << "\n";
    std::cout << "data (stored pointer): " << *(pshared.get()) << "\n";

    output("Naked pointer: ", pint);
//    output("Shared pointer ", pshared);
    output("Shared pointer with get(): ", pshared.get());

    delete pint;

    return 0;
}

/home/yongqiang/CLionProjects/yongqiang/cmake-build-debug/yongqiang
data type: Pi
data type (owned pointer): St10shared_ptrIiE
data type (stored pointer): Pi
data: 10
data (owned pointer): 9
data (stored pointer): 9
Naked pointer: 10
Shared pointer with get(): 9

Process finished with exit code 0

2. Windows

2.1 cannot convert argument 2 from 'std::shared_ptr' to 'const int *'

/*
============================================================================
Name        : yongqiang.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#include 
#include 
#include 
#include 

void output(std::string msg, const int *pint) {
	std::cout << msg << *pint << "\n";
}

int main() {
	int *pint = new int(10);
	std::shared_ptr pshared = std::make_shared(9);

	std::cout << "data type: " << typeid(pint).name() << std::endl;
	std::cout << "data type (owned pointer): " << typeid(pshared).name() << std::endl;
	std::cout << "data type (stored pointer): " << typeid(pshared.get()).name() << std::endl;
	std::cout << "data: " << *pint << "\n";
	std::cout << "data (owned pointer): " << *pshared << "\n";
	std::cout << "data (stored pointer): " << *(pshared.get()) << "\n";

	output("Naked pointer: ", pint);
	output("Shared pointer ", pshared);
	output("Shared pointer with get(): ", pshared.get());

	delete pint;

	return 0;
}

1>------ Build started: Project: yongqiangcheng, Configuration: Debug Win32 ------
1>  yongqiang.cpp
1>d:\visual_studio_workspace\yongqiangcheng\yongqiangcheng\yongqiang.cpp(32): error C2664: 'void output(std::string,const int *)': cannot convert argument 2 from 'std::shared_ptr' to 'const int *'
1>  d:\visual_studio_workspace\yongqiangcheng\yongqiangcheng\yongqiang.cpp(32): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2.2 stored pointer - owned pointer

/*
============================================================================
Name        : yongqiang.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#include 
#include 
#include 
#include 

void output(std::string msg, const int *pint) {
	std::cout << msg << *pint << "\n";
}

int main() {
	int *pint = new int(10);
	std::shared_ptr pshared = std::make_shared(9);

	std::cout << "data type: " << typeid(pint).name() << std::endl;
	std::cout << "data type (owned pointer): " << typeid(pshared).name() << std::endl;
	std::cout << "data type (stored pointer): " << typeid(pshared.get()).name() << std::endl;
	std::cout << "data: " << *pint << "\n";
	std::cout << "data (owned pointer): " << *pshared << "\n";
	std::cout << "data (stored pointer): " << *(pshared.get()) << "\n";

	output("Naked pointer: ", pint);
	// output("Shared pointer ", pshared);
	output("Shared pointer with get(): ", pshared.get());

	delete pint;

	return 0;
}

data type: int *
data type (owned pointer): class std::shared_ptr
data type (stored pointer): int *
data: 10
data (owned pointer): 9
data (stored pointer): 9
Naked pointer: 10
Shared pointer with get(): 9
请按任意键继续. . .

References

http://www.cplusplus.com/reference/typeinfo/

你可能感兴趣的:(C++)