Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 默认模板参数中的MPL占位符替换_C++_Template Meta Programming_Boost Mpl - Fatal编程技术网

C++ 默认模板参数中的MPL占位符替换

C++ 默认模板参数中的MPL占位符替换,c++,template-meta-programming,boost-mpl,C++,Template Meta Programming,Boost Mpl,我在理解MPL占位符时遇到一些问题。 有人能解释一下为什么这段代码无法编译吗 我希望打印数字0、1和2,但当编译器试图确定包装器的默认模板参数的类型时,占位符似乎没有被实际类型替换 #include <iostream> #include <boost/mpl/inherit.hpp> #include <boost/mpl/inherit_linearly.hpp> #include <boost/mpl/int.hpp> #include &l

我在理解MPL占位符时遇到一些问题。
有人能解释一下为什么这段代码无法编译吗

我希望打印数字0、1和2,但当编译器试图确定包装器的默认模板参数的类型时,占位符似乎没有被实际类型替换

#include <iostream>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

template <typename T> struct Traits;

template<> struct Traits<int>  { typedef boost::mpl::int_<0> type; };
template<> struct Traits<char> { typedef boost::mpl::int_<1> type; };
template<> struct Traits<bool> { typedef boost::mpl::int_<2> type; };

template <typename T, typename Type=typename Traits<T>::type > struct Wrapper
{
    Wrapper() { std::cout << "Value: " << Type::value << std::endl; }

    T value;
};

int main()
{
    typedef boost::mpl::inherit_linearly<
                boost::mpl::vector<int, char, bool>,
                boost::mpl::inherit<boost::mpl::_1, Wrapper<boost::mpl::_2> > >::type Object;

    Object obj;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
模板结构特征;
模板结构特征{typedef boost::mpl::int_uType;};
模板结构特征{typedef boost::mpl::int_uType;};
模板结构特征{typedef boost::mpl::int_uType;};
模板结构包装器
{
包装器(){std::cout::type对象;
^~~~~~~~~~~~~~~~~~~~~~~
test.cpp:9:34:注意:此处声明了模板
模板结构特征;

我对您的代码做了一点修改,将默认模板参数导致的
typedef
拆分为两个更简单的
typedef
s

考虑这一点:

template < typename T , typename Super_Type = Traits<T> > struct Wrapper
{ 
     typedef typename Super_Type::type Type;
     Wrapper() { std::cout << "Value: " << Type::value << std::endl; }

     T value;
};
templatestruct Wrapper
{ 
typedef typename Super_Type::Type Type Type;

Wrapper(){std::cout好吧……我好像已经破解了它……
通常,一个额外的抽象层次起到了作用

我创建了一个元函数类(genWrapper),它与占位符一起传递给mpl::apply1

这是生成的程序(第二版)。
谢谢大家的帮助和指点

#include <iostream>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

enum WrapperType { TYPE_A, TYPE_B };

template <typename T> struct Traits;

template<> struct Traits<int>  { typedef boost::mpl::int_<TYPE_A> type; };
template<> struct Traits<char> { typedef boost::mpl::int_<TYPE_B> type; };
template<> struct Traits<bool> { typedef boost::mpl::int_<TYPE_A> type; };

template <typename T, typename Type = typename Traits<T>::type> struct Wrapper;

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_A> >
{
    Wrapper() : value (0) { std::cout << "TYPE_A" << std::endl; }

    T value;
};

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_B> >
{
    Wrapper() { std::cout << "TYPE_B" << std::endl; }

    T value;
};

struct genWrapper
{
    template <typename T>
    struct apply
    {
        typedef Wrapper<T> type;
    };
};

int main()
{
    typedef boost::mpl::inherit_linearly<
                boost::mpl::vector<int, char, bool>,
                boost::mpl::inherit<boost::mpl::_1, boost::mpl::apply1<genWrapper, boost::mpl::_2> > >::type Object;

    Object obj;

    return 0;
}

关于它的价值,我有两个建议:1.你不是忘了做
typedef typename
而不是只做
typename
?2.你不需要
包装器
构造函数声明中的
。@Acorbe:
typename
只需要依赖名称,这里不是这样的。不过第二点是有效的,在构造函数IIRC中说
实际上是一个错误。@Acorbe@Xeo:谢谢你的回复。我不知道
无效。我已经从构造函数声明中删除了它,这减少了一点错误。遗憾的是,“未定义类型的无效使用”结构特征“”仍然存在。@CodeWarrior,很好。请更新代码和错误列表。@Acorbe:Original post updated:-)谢谢!这确实可以编译,但不适合我想做的。我希望能够对第二个模板参数的包装器进行部分专门化。似乎我过于简化了我的示例。我将更新我的原始post以显示我的目标…我试图用g编译这个++-4.5.4和CLAN++- 3.1,并得到类似的错误信息。我认为代码对于编译器来说太复杂了,但更多的是我还没有掌握MPL占位符的复杂之处。考虑到现在你正在严重地改变你的问题。请把你的新代码放进一个新的问题,或者至少,在一个“编辑”中。你的行为,否则,对那些自愿阅读和处理你的代码的人来说是不礼貌的。这不是它的工作方式。考虑这个Meta。StaskOfFult.COM/Quass/2081/So-Fiel-Tim-Advor问题。谢谢你的帮助和回答。我想做的最后一件事是对那些试图帮助别人的人不敬。我。正如你所建议的,我把修改过的示例程序放在一个编辑部分。据我所知,原来的问题仍然存在,报告的问题仍然是相同的,因此我认为创建同一个问题的另一个问题会令人困惑。非常好,谢谢!好问题!我会尽快考虑它。
# g++ -I ../boost test.cpp -o test
test.cpp: In functie ‘int main()’:
test.cpp:37:79: fout: invalid use of incomplete type ‘struct Traits<mpl_::arg<2> >’
test.cpp:9:34: fout: declaration of ‘struct Traits<mpl_::arg<2> >’
test.cpp:37:79: fout: template argument 2 is invalid
test.cpp:37:81: fout: template argument 2 is invalid
test.cpp:37:83: fout: template argument 2 is invalid
test.cpp:37:91: fout: expected initializer before ‘Object’
test.cpp:39:9: fout: ‘Object’ was not declared in this scope
test.cpp:39:16: fout: expected ‘;’ before ‘obj’
test.cpp:15:50: error: implicit instantiation of undefined template 'Traits<mpl_::arg<2> >'
    template <typename T, typename Type=typename Traits<T>::type > struct Wrapper;
                                                 ^
test.cpp:37:57: note: in instantiation of default argument for 'Wrapper<mpl_::arg<2> >' required here
                    boost::mpl::inherit<boost::mpl::_1, Wrapper<boost::mpl::_2> > >::type Object;
                                                        ^~~~~~~~~~~~~~~~~~~~~~~
test.cpp:9:34: note: template is declared here
    template <typename T> struct Traits;
template < typename T , typename Super_Type = Traits<T> > struct Wrapper
{ 
     typedef typename Super_Type::type Type;
     Wrapper() { std::cout << "Value: " << Type::value << std::endl; }

     T value;
};
#include <iostream>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

enum WrapperType { TYPE_A, TYPE_B };

template <typename T> struct Traits;

template<> struct Traits<int>  { typedef boost::mpl::int_<TYPE_A> type; };
template<> struct Traits<char> { typedef boost::mpl::int_<TYPE_B> type; };
template<> struct Traits<bool> { typedef boost::mpl::int_<TYPE_A> type; };

template <typename T, typename Type = typename Traits<T>::type> struct Wrapper;

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_A> >
{
    Wrapper() : value (0) { std::cout << "TYPE_A" << std::endl; }

    T value;
};

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_B> >
{
    Wrapper() { std::cout << "TYPE_B" << std::endl; }

    T value;
};

struct genWrapper
{
    template <typename T>
    struct apply
    {
        typedef Wrapper<T> type;
    };
};

int main()
{
    typedef boost::mpl::inherit_linearly<
                boost::mpl::vector<int, char, bool>,
                boost::mpl::inherit<boost::mpl::_1, boost::mpl::apply1<genWrapper, boost::mpl::_2> > >::type Object;

    Object obj;

    return 0;
}
# g++4 -I ../boost test.cpp -o test
# ./test
TYPE_A
TYPE_B
TYPE_A