Skip to content

Unable to return a const reference on the interface #37

Description

@suy

I expected to be able to return a const reference to a non-copyable object, like std::type_info:

struct Introspectable : te::poly<Introspectable> {
    const std::type_info& type() const {
        return te::call<const std::type_info&>([](auto const& self) {
            return typeid(self);
        }, *this);
    }
};

But I got this compilation errors:

Compile error (GCC 9.4):

In file included from main.cpp:4:
te.hpp: In instantiation of ‘constexpr auto boost::ext::te::v1::detail::call_impl(const boost::ext::te::v1::detail::poly_base&, std::integral_constant<long unsigned int, _Nm>, boost::ext::te::v1::detail::type_list<R>, TExpr, Ts&& ...) [with I = boost_te::Introspectable; long unsigned int N = 1; R = const std::type_info&; TExpr = boost_te::Introspectable::type() const::<lambda(const auto:3&)>; Ts = {}]’:
te.hpp:554:30:   required from ‘constexpr auto boost::ext::te::v1::call(TExpr, const I&, Ts&& ...) [with R = const std::type_info&; long unsigned int N = 0; TExpr = boost_te::Introspectable::type() const::<lambda(const auto:3&)>; I = boost_te::Introspectable; Ts = {}]’
main.cpp:18:17:   required from here
te.hpp:522:44: error: ‘std::type_info::type_info(const std::type_info&)’ is private within this context
  522 |       self.ptr(), std::forward<Ts>(args)...);
      |                                            ^
In file included from /usr/include/c++/9/bits/exception_ptr.h:39,
                 from /usr/include/c++/9/exception:143,
                 from /usr/include/c++/9/ios:39,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/typeinfo:178:5: note: declared private here
  178 |     type_info(const type_info&);
      |     ^~~~~~~~~
main.cpp: In member function ‘const std::type_info& boost_te::Introspectable::type() const’:
main.cpp:16:47: warning: returning reference to temporary [-Wreturn-local-addr]
   16 |         return te::call<const std::type_info&>([](auto const& self) {
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
   17 |             return typeid(self);
      |             ~~~~~~~~~~~~~~~~~~~~               
   18 |         }, *this);
      |         ~~~~~~~~~                              

Possibly related: #13

Thank you!

Compile error (clang 14):

In file included from main.cpp:4:
./te.hpp:521:10: error: calling a private constructor of class 'std::type_info'
  return reinterpret_cast<R (*)(void *, Ts...)>(self.vptr[N - 1])(
         ^
./te.hpp:554:18: note: in instantiation of function template specialization 'boost::te::detail::call_impl<boost_te::Introspectable, 1, const std::type_info &, (lambda at main.cpp:16:48)>' requested here
  return detail::call_impl<I>(
                 ^
main.cpp:16:20: note: in instantiation of function template specialization 'boost::te::call<const std::type_info &, 0, (lambda at main.cpp:16:48), boost_te::Introspectable>' requested here
        return te::call<const std::type_info&>([](auto const& self) {
                   ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/typeinfo:181:5: note: declared private here
    type_info(const type_info&);
    ^
main.cpp:16:16: warning: returning reference to local temporary object [-Wreturn-stack-address]
        return te::call<const std::type_info&>([](auto const& self) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
  • Version: latest te.hpp (2746584)
  • Platform: Linux, GCC or clang
  • Subsystem: (?)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions