模板构造函数的奇怪之处

发布于 2024-11-15 16:45:30 字数 1416 浏览 0 评论 0原文

可能的重复:
可以显式指定构造函数的模板参数吗?< /a>

跟进我之前的问题, (我在编辑2中发现了这种情况)

代码简单:

#include <iostream>

struct Printer
{
  Printer() { std::cout << "secret code" << std::endl; }
};

template <class A>
struct Class
{
  template <class B, class C>
  Class(B arg)
  {
      C c; /* the 'secret code' should come from here */
      std::cout << arg << std::endl;
  }

  Class(double arg) { std::cout << "double" << std::endl; }
  Class(float arg) { std::cout << "float" << std::endl; }

  /* this forbids the use of printer in the first parameter */
  Class(Printer printer) { throw std::exception(); /* here be dragons */ }
};

int main()
{
  Class<int> c(1.0f);
  Class<int>* ptr = new Class<int>((double)2.0f);
  return 0;
}

// Can anyone print 'secret code' while creating an object of type 'Class' ?

详细:对于模板构造函数,当实例化对象时,您可以指定一个不属于构造函数参数的模板参数吗?

我认为这本身就值得一个问题。

Possible Duplicate:
Can the template parameters of a constructor be explicitly specified?

following up on my previous question, (I found this situation in edit 2)

Laid out simple in code:

#include <iostream>

struct Printer
{
  Printer() { std::cout << "secret code" << std::endl; }
};

template <class A>
struct Class
{
  template <class B, class C>
  Class(B arg)
  {
      C c; /* the 'secret code' should come from here */
      std::cout << arg << std::endl;
  }

  Class(double arg) { std::cout << "double" << std::endl; }
  Class(float arg) { std::cout << "float" << std::endl; }

  /* this forbids the use of printer in the first parameter */
  Class(Printer printer) { throw std::exception(); /* here be dragons */ }
};

int main()
{
  Class<int> c(1.0f);
  Class<int>* ptr = new Class<int>((double)2.0f);
  return 0;
}

// Can anyone print 'secret code' while creating an object of type 'Class' ?

Detailed: For a template constructor, can you specify a template argument which is not part of the constructor's arguments when an object get's instantiated?

I think this deserves a question of its own.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-11-22 16:45:30

不,这是不可能的。

没有任何语法可以为构造函数模板提供显式模板参数。您只能为整个类模板提供显式模板参数。

以下来自 [temp.arg.explicit] 的文本(2003 年措辞,14.8.1/5)涵盖了该场景。尽管该子句是非规范性的,但它向我们解释,作为语法的固有限制,这是不可能的:

注意:因为显式模板
参数列表位于函数之后
模板名称和因为转换
成员函数模板和
构造函数成员函数模板
不使用函数来调用
名称
无法提供
显式模板参数列表
这些功能模板

这在一定程度上是由于您自己从未真正显式调用构造函数这一事实。例如,当您编写 A() 时,您并没有像函数一样调用构造函数,即使它看起来好像是(“调用转换成员函数模板和构造函数成员函数模板时不使用函数名称”)。

No, it's not possible.

There is no syntax with which you can provide explicit template parameters to a constructor template. You can only provide explicit template parameters for the class template as a whole.

The following text from [temp.arg.explicit] (2003 wording, 14.8.1/5) covers the scenario. Though the clause is non-normative, it serves to explain to us that, as an inherent restriction of the grammar, this is not possible:

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
.

This, partially, comes out of the fact that you never actually invoke the constructor explicitly yourself. When you write, say, A() you are not calling the constructor like a function, even though it looks as if you are ("conversion member function templates and constructor member function templates are called without using a function name").

少钕鈤記 2024-11-22 16:45:30

我想他想知道如何用 C 实例化这个类作为 SomeType:

template<typename A>
class foo
{
    template<typename B, typename C>
    foo(B b)
    {
        C c;
    }
};

我不知道这是否可能。

I think he want to know how to instantiate this class with C as SomeType:

template<typename A>
class foo
{
    template<typename B, typename C>
    foo(B b)
    {
        C c;
    }
};

I don't know if this is possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文