std::forward : 'forward' is not a member of 'std'

Good evening everybody,

I am pretty new to C++, and still seem to be very attracted to pitfalls. Inspired by this article(http://enki-tech.blogspot.ch/2012/08/c11-generic-singleton.html), I tried to implement a Singleton template class :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <utility>

template <class T> class Singleton {


	private:
		static T* s_instance;

	public:
	
		template <typename B> static T& create(B args){
			s_instance = new T(std::forward<B>(args));
			return getInstance();	
		};




};

template <class T> T* Singleton<T>::s_instance = 0;

}


I simplified it a bit with the hope it would work, but unfortunately I keep getting the same error : 'forward' is not a member of 'std'. I am using g++ 4.7 through CMake, on a Debian system. The 'utility' file does exist, and contains references to std::forward. Any guess about what's wrong?

Thanks,
Nicolas
By default, g++ compiles a home-grown GNU dialect of the superseded C++ standard.

We have to explicitly ask for standard C++ conformance with -std=c++11 in conjunction with -pedantic-errors.

Perfect forwarding:
1
2
3
4
5
// template <typename B> static T& create(B args){
   template <typename... B> static T& create( B&&... args ) {
                        // s_instance = new T(std::forward<B>(args));
			   if( !s_instance ) s_instance = new T( std::forward<B>(args)... ); 
                        // ... 


For simple, non-polymorphic singletons, favour a Meyers' singleton (thread-safe).
http://stackoverflow.com/questions/1661529/is-meyers-implementation-of-singleton-pattern-thread-safe

Note: The singleton pattern is most useful when the singleton object is exposed via a polymorphic interface.
Last edited on
Thanks for the quick answer.

What threw me off, is that when I add variadic arguments, I get the following warning:
Singleton.hpp:18:21: warning: variadic templates only available with -std=c++11 or -std=gnu++11 [enabled by default] and assumed c++11 was enabled.

It now turns into an error if I explicitely add -std=c++11 and completely disappears with the combo -std=c++11 -pedantic-errors. Thanks !
Last edited on
> It now turns into an error if I explicitely add -std=c++11

It shouldn't. http://coliru.stacked-crooked.com/a/aa404e797e03a679
Topic archived. No new replies allowed.