Igor
Igor

Reputation: 6275

Initializing unque_ptr

ALL,

Here is my "naive" try:

std::unique_ptr<SQLWCHAR> ptr;
ptr = std::make_unique<SQLWCHAR>( "INSERT INTO mytable VALUES();" );

which results in the compilation error.

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

What is a proper way of initializing it like this?

EDIT:

I tried to build the following on Linux with unixODBC and got the following:

Code:

std::unique_ptr<SQLWCHAR *> ptr;
ptr = std::make_unique<SQLWCHAR *>( L"INSERT INTO mytable VALUES();" );

The errors are:

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

EDIT2:

SINCE THE QUESTION IS INCORRECTLY CLOED I GURDS ILL CLOSE THIS BY EDITING AN OP...

I refactoring the code and so right now I don't need a smart pointer. The code looks much better and .ore maintainable

I just thought that someone will help me understand smart pointers, but unfortunately this is not the case.

Sorry for the noise...

Upvotes: 0

Views: 68

Answers (0)

Related Questions