H
huili80
Can anyone please tell me what I'm doing wrong in the stripped-down code below?
I can't figure out the cause of failure when compiling, tried both clang++4.1 and g++4.8 with -std=c++11.
Thanks very much!!
----------------------------------------------------
#include <utility>
namespace test {
template < typename D > struct base{};
struct MyP : base<MyP> { explicit MyP(int){} };
template < typename T >
struct remove_cvr
{
typedef typename std::remove_cv< typename std::remove_reference<T>::type >::type type;
};
template < typename... P >
struct array_impl;
// single element, recursive root
template < typename P >
struct array_impl<P>
{
P head_;
template < typename Q > explicit array_impl(Q&& q):head_(q){} // fail to compile, why?
};
template < typename P, typename... T >
struct array_impl<P,T...>
{
P head_; array_impl<T...> tail_;
template < typename Q, typename... U > explicit array_impl(Q&& q, U&&... u):head_(std::forward<Q>(q)),tail_(std::forward<U>(u)...) {}
};
template < typename... P >
struct array : public array_impl<P...>, public base<array<P...>>
{
typedef array_impl<P...> impl_base;
template < typename... Q > explicit array(Q&&... q):impl_base(std::forward<Q>(q)...){}
};
template < typename... P >
array<typename remove_cvr<P>::type...> make_array(P&&... p)
{
return array<typename remove_cvr<P>::type...>(std::forward<P>(p)...);
}
//// compiles fine if not trying to forward
//template < typename... P >
//array<typename remove_cvr<P>::type...> make_array(const P&... p)
//{
// return array<typename remove_cvr<P>::type...>(p...);
//}
}
int main(int argc, const char * argv[])
{
using namespace test;
// make_array(MyP(3.4)); // compiles fine
make_array(make_array(MyP(3.4))); // failed both g++4.8 and clang++4.1 (mac osx 10.8.2)
return 0;
}
I can't figure out the cause of failure when compiling, tried both clang++4.1 and g++4.8 with -std=c++11.
Thanks very much!!
----------------------------------------------------
#include <utility>
namespace test {
template < typename D > struct base{};
struct MyP : base<MyP> { explicit MyP(int){} };
template < typename T >
struct remove_cvr
{
typedef typename std::remove_cv< typename std::remove_reference<T>::type >::type type;
};
template < typename... P >
struct array_impl;
// single element, recursive root
template < typename P >
struct array_impl<P>
{
P head_;
template < typename Q > explicit array_impl(Q&& q):head_(q){} // fail to compile, why?
};
template < typename P, typename... T >
struct array_impl<P,T...>
{
P head_; array_impl<T...> tail_;
template < typename Q, typename... U > explicit array_impl(Q&& q, U&&... u):head_(std::forward<Q>(q)),tail_(std::forward<U>(u)...) {}
};
template < typename... P >
struct array : public array_impl<P...>, public base<array<P...>>
{
typedef array_impl<P...> impl_base;
template < typename... Q > explicit array(Q&&... q):impl_base(std::forward<Q>(q)...){}
};
template < typename... P >
array<typename remove_cvr<P>::type...> make_array(P&&... p)
{
return array<typename remove_cvr<P>::type...>(std::forward<P>(p)...);
}
//// compiles fine if not trying to forward
//template < typename... P >
//array<typename remove_cvr<P>::type...> make_array(const P&... p)
//{
// return array<typename remove_cvr<P>::type...>(p...);
//}
}
int main(int argc, const char * argv[])
{
using namespace test;
// make_array(MyP(3.4)); // compiles fine
make_array(make_array(MyP(3.4))); // failed both g++4.8 and clang++4.1 (mac osx 10.8.2)
return 0;
}