【问题标题】:Is there a way to recover original template template class enbeded in boost mpl quote?有没有办法恢复嵌入在 boost mpl 报价中的原始模板模板类?
【发布时间】:2014-07-21 01:19:36
【问题描述】:

我引用了一个模板类将其放入 mpl::vector 中:

boost::mpl::vector<int, boost::mpl::quote2<std::pair>>

然后,我得到了这样的第二个元素:

using A=typename boost::mpl::at<T, boost::mpl::int_<2>>::type;

我现在需要将原始模板类传递给这样的类:

template<class A, template<class, class> class C>
class B{
    C<A, B*> _c;
};

我尝试使用apply或bind,但找不到让B接受第二个参数的方法。

我遇到了这样的错误:

error: template argument for template template parameter must be a class template or type alias template

编辑:

示例代码:

#include <boost/mpl/vector.hpp>
#include <boost/mpl/quote.hpp>
#include <boost/mpl/at.hpp>

template<class, class> class A{};
template<class A, template<class, class> class C>
class B{
    C<A, B*> _c;
};
using T=boost::mpl::vector<int, boost::mpl::quote2<A>> ;
using T1=typename boost::mpl::at<T, boost::mpl::int_<0>>::type;
using T2=typename boost::mpl::at<T, boost::mpl::int_<1>>::type;

int main(){
    B<T1, T2> b;
    return 0;
}

我明白了:

error: template argument for template template parameter must be a class template or type alias template B<T1, T2> b;

【问题讨论】:

  • template class C > 应该只是 template
  • 不,实际上这是有效的。意思是您期望一个具有 2 个模板参数的模板类。不工作的代码是使用 mpl 进行引用的代码,我试图在之后取消引用。
  • 一个类模板,而不是一个模板类——这是辨别因素。 ;-]
  • 请发布一个最小且完整的示例来显示您的问题。此外,MPL 向量是 0 索引的,因此向量的第二个元素将是 mpl::at&lt;V, mpl::int_&lt;1&gt;&gt;::type 而不是 mpl::at&lt;V, mpl::int_&lt;2&gt;&gt;::type
  • 哦,对不起2,因为我的代码有3个参数,它想简化,忘了把2改成1。现在写一个完整的例子。

标签: c++ templates c++11 boost boost-mpl


【解决方案1】:

MPL 在很大程度上仍然是一个 C++03 库 AFAIK,您正试图让它生成一些在 C++11 之前概念上不存在的东西。我怀疑在这种情况下让quote 工作是语法的巧合,而不是预期的功能。

以下代码在VC2013中编译成功:

#include <boost/mpl/vector.hpp>
#include <boost/mpl/quote.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/at.hpp>

template<class, class> class A{};
template<class A, template<class, class> class C>
class B{
    C<A, B*> _c;
};
using T = boost::mpl::vector < int, boost::mpl::quote2<A> > ;
using T1 = boost::mpl::at<T, boost::mpl::int_<0>>::type;
using T2 = boost::mpl::at<T, boost::mpl::int_<1>>::type;

template<typename X1, typename X2>
using TT2 = typename boost::mpl::apply<T2, X1, X2>::type;

int main(int argc, char* argv[])
{
    B<T1, TT2> b;
    return 0;
}

【讨论】:

  • 是的,这是我的第一个猜测 :) 我收到关于 clang 说“模板需要模板后的符号”或类似内容的错误,将其追踪到错误报告中说“模板别名”不是充分工作。我正在将 clang 更新为 svn 版本,以查看是否可行,同时我正在尝试找出替代解决方案。
  • 鉴于您的示例代码,我已更新我的答案以包含一个工作(或至少编译)版本。尽管没有在 clang 上进行测试,并且标识符名称很糟糕。 :)
  • 这很有趣,它在 main 之外定义时有效。我试图在 BOOST_AUTO_TEST_CASE_TEMPLATE 中使用相同的代码并得到:“错误:预期表达式”,如果我将 usings 移动到 main 中,则相同,错误是由 using TT2 行触发的。
  • 最新版本的 clang 并没有让它变得更好。
  • 现在看起来像死路一条,反正回复回答了原来的问题,谢谢!。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 2012-12-02
相关资源
最近更新 更多