如何正确初始化存储SQL语句的std::unique_ptr<SQLWCHAR>?
问题:std::unique_ptr初始化SQLWCHAR字符串的编译错误
我的初步尝试代码:
std::unique_ptr<SQLWCHAR> ptr; ptr = std::make_unique<SQLWCHAR>( "INSERT INTO mytable VALUES();" );
这段代码出现编译错误:
1> c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\memory(2539): error C2440: 'initializing': cannot convert from 'const char [380]' to '_Ty' 1> with 1> [ 1> _Ty=SQLWCHAR 1> ] 1> c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\memory(2539): note: There is no context in which this conversion is possible
请问正确的初始化方式是什么?
编辑补充:Linux环境下的尝试
我在Linux环境下使用unixODBC尝试了以下代码:
std::unique_ptr<SQLWCHAR *> ptr; ptr = std::make_unique<SQLWCHAR *>( L"INSERT INTO mytable VALUES();" );
得到如下错误:
error: no matching function for call to 'make_unique(const wchar_t [23])' In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/memory:78, from ../../libodbc/database_odbc.cpp:20: /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/unique_ptr.h:1069:5: note: candidate: 'template<class _Tp, class ... _Args> std::__detail::__unique_ptr_t<_Tp> std::make_unique(_Args&& ...)' 1069 | make_unique(_Args&&... __args) | ^~~~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/unique_ptr.h:1069:5: note: template argument deduction/substitution failed: ../../libodbc/database_odbc.cpp:2275:31: note: couldn't deduce template parameter '_Tp' 2275 | ptr = std::make_unique( L"INSERT INTO abcattbl ;" ); | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/unique_ptr.h:1084:5: note: candidate: 'template<class _Tp> std::__detail::__unique_ptr_array_t<_Tp> std::make_unique(size_t)' 1084 | make_unique(size_t __num) | ^~~~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/unique_ptr.h:1084:5: note: template argument deduction/substitution failed: ../../libodbc/database_odbc.cpp:2275:31: note: couldn't deduce template parameter '_Tp' 2275 | ptr = std::make_unique( L"INSERT INTO abcattbl ;" ); | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/unique_ptr.h:1094:5: note: candidate: 'template<class _Tp, class ... _Args> std::__detail::__invalid_make_unique_t<_Tp> std::make_unique(_Args&& ...)' (deleted) 1094 | make_unique(_Args&&...) = delete; | ^~~~~~~~~~~ /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/unique_ptr.h:1094:5: note: template argument deduction/substitution failed: ../../libodbc/database_odbc.cpp:2275:31: note: couldn't deduce template parameter '_Tp' 2275 | ptr = std::make_unique( L"INSERT INTO abcattbl ;" ); | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ make[2]: *** [Makefile:531: libodbc_lib_la-database_odbc.lo] Error 1 make[2]: Leaving directory '/home/igor/dbhandler/Debug/libodbc' make[1]: *** [Makefile:526: all-recursive] Error 1 make[1]: Leaving directory '/home/igor/dbhandler/Debug' make: *** [Makefile:437: all] Error 2
编辑补充2
由于问题被错误关闭,我将通过编辑原帖结束此问题。
我重构了代码,现在不需要智能指针了,代码更简洁易维护。
我本希望有人能帮我理解智能指针,但未能如愿,抱歉打扰大家。
内容的提问来源于stack exchange,提问作者Igor
相关产品推荐
相关产品推荐

