c ++ 11:委托构造函数-无法选择构造函数模板? [英] c++11: delegate constructor - can't select constructor template?

查看:83
本文介绍了c ++ 11:委托构造函数-无法选择构造函数模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对以下问题的跟踪问题:

c ++ 11专用的代理构造函数委托给私有的通用引用构造函数?

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

我想摆脱那里使用的枚举类Dummy。

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");


推荐答案

这并非专门委派构造函数。构造函数(和转换函数)没有名称,因此语言中无法为它们提供显式模板参数。引用C ++ 14,[temp.mem] 14.5.2 / 5:

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 ]

注释不是规范性的,但此注释仅明确说明了整个第14章都有规定。

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

这篇关于c ++ 11:委托构造函数-无法选择构造函数模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆