5

I am trying to create thing similar to tuple, but I have come across a problem for writing my constructor.

Here is the code:

#include <tuple>

template <typename... Ts>
struct B {
    template <typename... ArgTypes>
    explicit B(ArgTypes&&... args)
    {
        static_assert(sizeof...(Ts) == sizeof...(ArgTypes),
            "Number of arguments does not match.");
    }
};

struct MyType {
    MyType() = delete;
    MyType(int x, const char* y) {}
};

int main()
{
   B         <int, char>               a{2, 'c'};                      // works
   B         <int, bool, MyType, char> b{2, false, {4, "blub"}, 'c'};  // fails
   std::tuple<int, bool, MyType, char> t{2, false, {4, "blub"}, 'c'};  // works
}

Now, this works ok if pass simple types as initializers, but it does not, if I try to pass arguments in a brace-enclosed initializer list for non-trivial object.

GCC-4.7 emits the following:

vararg_constr.cpp:21:67: error: no matching function for call to 'B<int, bool, MyType, char>::B(<brace-enclosed initializer list>)'
vararg_constr.cpp:21:67: note: candidates are:
vararg_constr.cpp:6:14: note: B<Ts>::B(ArgTypes&& ...) [with ArgTypes = {}; Ts = {int, bool, MyType, char}]
vararg_constr.cpp:6:14: note:   candidate expects 0 arguments, 4 provided

Clang-3.1 the following:

vararg_constr.cpp:21:40: error: no matching constructor for initialization of
      'B<int, bool, MyType, char>'
   B         <int, bool, MyType, char> b{2, false,{4, "blub"}, 'c'};  // fails
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
vararg_constr.cpp:6:14: note: candidate constructor not viable: requires 2
      arguments, but 4 were provided
    explicit B(ArgTypes&&... args)

Ok, now what makes me very, very curious is that it works for tuple! According to the Standard (20.4.2.1) it has a constructor, that looks pretty much like mine.

template <class... Types>
class tuple {
public:
    // ...

    template <class... UTypes>
    explicit tuple(UTypes&&...);

    // ...
};

When constructing the tuple object in the same way, it works!

Now I would like to know:

A) What the hell? Why is std::tuple so special, and why don't compilers deduce the correct number of arguments?

B) How can I make this work?

1 Answer 1

6

A) Why should the Compiler know, that {4, "blub"} is of type MyType and not tuple<int, const char*>?

B) Change ArgTypes to Ts in the constructor:

explicit B(Ts&&... args)

Tuple does also have the following constructor:

  explicit constexpr tuple(const _Elements&... __elements);

EDIT: The point is, that the constructor with const& is called and not that with the R-Values. Consider the following:

template <typename... Ts>
struct B {
  explicit B(const Ts&... elements) { std::cout << "A\n"; }
  template<typename... As,
           typename = typename std::enable_if<sizeof...(As) == sizeof...(Ts)>::type>
  explicit B(As&&... elements) { std::cout << "B\n" ;}
};

int main()
{
  MyType m {1, "blub"};
  B<int, char>           a{2, 'c'};                            // prints B
  B<bool, MyType, char>  b{false, {4, "blub"}, 'c'};           // prints A
  B<bool, MyType, MyType>c{false, {4, "blub"}, std::move(m)};  // prints A
}
3
  • A) How does the compiler know for the tuple constructor that it's a tuple and not MyType? B) Sorry, I should have elaborated on tuple a little. Tuple is parametrized on Ts..., but the constructor I am talking about is additionally parametrized on UTypes... .
    – fat-lobyte
    Mar 3, 2012 at 21:54
  • 1
    Note that for part A, putting && on the parameters of the enclosing template changes the meaning so it's no longer perfect forwarding, but will (barring reference arguments to Ts) form rvalue references, which are not desired. For part B, the rvalue references are "corrected" to lvalue const & so it will usually work, but it's still not perfect forwarding. Mar 4, 2012 at 7:43
  • To expand on what Potatoswatter said, the usual solution is to write a constructor taking T... t, and then initialize the members from std::forward<T>(t).... To convince yourself that it works, 'instantiate' the template (in your head :p) with T being a type, an lvalue reference, an rvalue reference and with its initializer being an lvalue then an rvalue. All situations will work and only the one where T is a type initialized from an lvalue results in a situation where one construction is superfluous. To avoid that means writing more than one constructor with a heavy dose of SFINAE.
    – Luc Danton
    Mar 4, 2012 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.