typeof
is a gcc extension and is not part of the C++ standard.typeof
deprecated in C++11? in other words, is it allowed to still use it as a gcc extension when using C++11?typeof
with decltype
yields the same behaviour of the code? template<typename T> class wrapper
. What is the best way to declare wrapper_some_field
such that it is equivalent to: Wrapper<typeof(some_field)> wrapper_some_field
Is the word
typeof
deprecated in C++11? in other words, is it allowed to still use it as a gcc extension when using C++11?
It's not deprecated. It never existed as a keyword. gcc suggests that if you compile with -std=c++**
that you instead use __typeof__
.
Is it correct to say that replacing every
typeof
withdecltype
yields the same behaviour of the code?
No. For example, given:
int& foo();
decltype(foo())
is int&
but __typeof__(foo())
is int
.
Assume I have
template<typename T> class wrapper
. [...]
You could write wrapper<std::remove_reference_t<decltype(some_field)>> wrap{some_field}
, but it'd be cleaner to write a construction function template:
template <class T> wrapper<T> make_wrapper(T const& val) { return {val}; }
auto wrap = make_wrapper(some_field);
Or, with forwarding:
template <class T>
wrapper<std::decay_t<T>> make_wrapper(T&& val) {
return {std::forward<T>(val)};
}
Although in C++17 you wouldn't do this at all and would just use class template argument deduction:
template <class T> struct wrapper { T t; };
template <class T> wrapper(T) -> wrapper<T>;
auto wrap = wrapper{42}; // wrap is a wrapper<int>
And in C++20, you won't even need the deduction guide.