Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++11: delegate constructor - can't select constructor template?

Tags:

c++

c++11

This is a follow up question to:
c++11 dedicated "proxy constructors" delegating to private univeral reference constructor?

I would like to get rid off the "enum class Dummy" used there.

But i don't manage to delegate to a template constructor.
See code example below.

#include <iostream>
#include <string>
#include <typeinfo>

class MyClass
{

private:

    template <class T>
    MyClass(T&& data)
    : _data(std::forward<T>(data))
    {
        std::cout << "MyClass universal reference template c'tor" << std::endl;
    }

public:


    // proxy c'tors delegating to universal reference c'tor
    MyClass (std::string const & data)
    : MyClass<std::string>(data)
    {
        std::cout << "MyClass lvalue c'tor" << std::endl;
    }

    MyClass (std::string && data)
    : MyClass<std::string>(std::move(data))
    {
        std::cout << "MyClass rvalue c'tor" << std::endl;
    }

private:

    std::string _data;

};

int
main(
        int,
        char**)
{

    {
        std::string str("demo");
        MyClass myClass(str);
    }

    {
        MyClass myClass("hello, world");
    }

    return 0;
}

I get following error:

main2.cpp: In constructor 'MyClass::MyClass(const string&)':
main2.cpp:21:7: error: 'class MyClass MyClass::MyClass' is not a non-static data member of 'MyClass'
 : MyClass<std::string>(data)
   ^
main2.cpp:21:14: error: expected '(' before '<' token
 : MyClass<std::string>(data)
          ^
main2.cpp:21:14: error: expected '{' before '<' token
main2.cpp: In constructor 'MyClass::MyClass(std::__cxx11::string&&)':
main2.cpp:27:7: error: 'class MyClass MyClass::MyClass' is not a non-static data member of 'MyClass'
 : MyClass<std::string>(std::move(data))
   ^
main2.cpp:27:14: error: expected '(' before '<' token
 : MyClass<std::string>(std::move(data))
          ^
main2.cpp:27:14: error: expected '{' before '<' token
main2.cpp: In function 'int main(int, char**)':
main2.cpp:11:5: error: 'MyClass::MyClass(T&&) [with T = std::__cxx11::basic_string<char>&]' is private
 MyClass(T&& data)
 ^
main2.cpp:46:28: error: within this context
     MyClass myClass(str);
                        ^
main2.cpp:11:5: error: 'MyClass::MyClass(T&&) [with T = const char (&)[13]]' is private
 MyClass(T&& data)
 ^
main2.cpp:50:39: error: within this context
     MyClass myClass("hello, world");
like image 246
Frank Bergemann Avatar asked Mar 11 '23 17:03

Frank Bergemann


1 Answers

That is not specific to delegating constructors. Constructors (and conversion functions) do not have names, and thus there is no way in the langauge to supply explicit template arguments to them. Quoting C++14, [temp.mem] 14.5.2/5:

[ Note: Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. —end note ]

Notes are not normative, but this note just explicitly spells out what follows from the rules throughout chapter 14.

like image 74
Angew is no longer proud of SO Avatar answered Apr 27 '23 22:04

Angew is no longer proud of SO