make_unique

closed account (SECMoG1T)
Hello , is there a way i can make this function supported by Gcc, everytime i need it i hve to provide it's definition , and some times i get alot of errors

Last edited on
closed account (SECMoG1T)
ohh thank you @mutexe but i have my gcc suport c++ 11- Gnu gcc 4.9.2 by default so that might not be the issue , the problem is i can't use std::make_unique if i need it i'll have to define a function n thet is quite problematic .
what error messages do you get if you try and compile this:
1
2
3
4
5
6
7
#include<memory>

int main()
{
	std::unique_ptr<int> pPointer = std::make_unique<int>();
	return 0;
}


?
Last edited on
closed account (SECMoG1T)

||=== Build: Debug in eostro (compiler: GNU GCC Compiler) ===|
C:\Users\Orion\Desktop\p.l\eostro\tester.cpp|17|error: 'make_unique' is not a member of 'std'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|



if i need it i'll provide this def and use it as an alternative, and that's what i want to avoid

1
2
3
4
5
template< class T, class... ARGS >
std::unique_ptr<T> make_unique( ARGS&&... my_args )
{
   return std::unique_ptr<T>(new T(std::forward<ARGS>(my_args)...));
}


Note : the function is supported on my vs 13, the problem is that sometimes it fails to build my projects for some unknown reasons and am forced to use gcc.
Last edited on
make_unique is not part of C++11 but c++14. See:

http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
Balls. My mistake sorry.
closed account (SECMoG1T)
oh yea it's a c++ 14 feature ,but what should i do next?
There is not much you can do. Either you look for a compiler that supports it or you doing it yourself and replace it as soon as it is available.
closed account (SECMoG1T)
ooh GOD! then maybe i should sort out my problem with vs - does anyone know what to do with this bug - i have tried a million solutions and none worked

but sometimes vs will works just fine.


Unable to start program 'c:\Users\Orion\documents\visual studio 2013 \Projects\BD_ST\Debug\BD_ST.exe'.

system can't find the file specified.


or what other compiler do you recommend , i thought GNU gcc 4.9.2 should support that.
Last edited on
Create a header which can be reused. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAKE_UNIQUE_H_INCLUDED
#define MAKE_UNIQUE_H_INCLUDED

#include <memory>
#include <utility>

namespace util
{
    #if __cplusplus == 201402L // C++14

        using std::make_unique ;

    #else // C++11

        template < typename T, typename... CONSTRUCTOR_ARGS >
        std::unique_ptr<T> make_unique( CONSTRUCTOR_ARGS&&... constructor_args )
        { return std::unique_ptr<T>( new T( std::forward<CONSTRUCTOR_ARGS>(constructor_args)... ) ); }

    #endif // __cplusplus == 201402L
}


#endif // MAKE_UNIQUE_H_INCLUDED 

http://coliru.stacked-crooked.com/a/cb9baad37307f5cd
closed account (SECMoG1T)
ooh thank you @JLBorges i did add that header but in my current project so it works so well when i use it like this:

1
2
3
4
5
6
7
#include "my_util.h"

int main()
{
   std::unique_ptr<int> ptr=util::make_unique<int>(10);
   std::cout<<*ptr<<std::endl;
}

10


Is it possible to make the util header part of the my IDE so that when i need to use it i'll not need to hunt around for the project containing the header? that will be a cool solution.
Last edited on
make_unique isn't necessary. You can always write it like so:

std::unique_ptr<int> ptr(new int(10));
closed account (SECMoG1T)
@coder777 thanks you very much.
Topic archived. No new replies allowed.