I've created a header for optionally-lazy parameters (also visible in a GitHub repository). (This is not my first question based on the header.)
I have a base-class template and two derived-class templates. The base-class template has a protected
constructor with a static_assert
. This constructor is only called by a particular derived-class. Inside of the static_assert
I'm using a decltype
.
The really bizarre thing is that the type of a name inside the decltype
is somehow affected by the whether or not there is a virtual destructor in my base-class template.
Here's my MCVE:
#include <type_traits>
#include <utility>
template <typename T>
class Base
{
protected:
template <typename U>
Base(U&& callable)
{
static_assert(
std::is_same<
typename std::remove_reference<decltype(callable())>::type, T
>::value,
"Expression does not evaluate to correct type!");
}
public:
virtual ~Base(void) =default; // Causes error
virtual operator T(void) =0;
};
template <typename T, typename U>
class Derived : public Base<T>
{
public:
Derived(U&& callable) : Base<T>{std::forward<U>(callable)} {}
operator T(void) override final
{
return {};
}
};
void TakesWrappedInt(Base<int>&&) {}
template <typename U>
auto MakeLazyInt(U&& callable)
{
return Derived<
typename std::remove_reference<decltype(callable())>::type, U>{
std::forward<U>(callable)};
}
int main()
{
TakesWrappedInt(MakeLazyInt([&](){return 3;}));
}
Note that if the destructor is commented out, this compiles without error.
The intent is for callable
to be an expression of type U
that, when called with the ()
operator, returns something of type T
. Without the virtual destructor in Base
, it appears that this is evaluated correctly; with the virtual destructor, it appears that callabele
's type is Base<T>
(which, as far as I can tell, makes no sense).
Here's G++ 5.1's error message:
recursive_lazy.cpp: In instantiation of ‘Base<T>::Base(U&&) [with U = Base<int>; T = int]’:
recursive_lazy.cpp:25:7: required from ‘auto MakeLazyInt(U&&) [with U = main()::<lambda()>]’
recursive_lazy.cpp:48:47: required from here
recursive_lazy.cpp:13:63: error: no match for call to ‘(Base<int>) ()’
typename std::remove_reference<decltype(callable())>::type, T
Here's Clang++ 3.7's error message:
recursive_lazy.cpp:13:55: error: type 'Base<int>' does not provide a call operator
typename std::remove_reference<decltype(callable())>::type, T
^~~~~~~~
recursive_lazy.cpp:25:7: note: in instantiation of function template specialization
'Base<int>::Base<Base<int> >' requested here
class Derived : public Base<T>
^
1 error generated.
EDIT: =delete
-ing the copy-constructor also triggers this error.
Derived(U&& callable)
takes an r-value reference and not a universal reference. Is that intended? – Lasandralasatercallable
probably should be an r-value reference. – HurtleU
in different parts of the same class). – Lasandralasatervirtual
and everything to do with a greedy unconstrained constructor that accepts everything under the sun. – Pisoliteenable_if
I could use to constrain it? Oddly enough (to me, at least), making the constructorsexplicit
does not help (I get the same error). Frankly, I feel like the "constructor from universal reference" feature of C++ is simply too hard to use correctly for my comfort. – Hurtleexplicit
fixed the issue in this case, then return-by-value would be be broken in many surprising ways.... – Hurtle