【问题标题】:Reducing template parameter with using or typedef使用 using 或 typedef 减少模板参数
【发布时间】:2017-11-13 22:44:52
【问题描述】:

我正试图通过引入“using”关键字来提高我的代码可读性。

namespace EigenRM
{
    template<typename T>
    using MatrixX<T> = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
}

此代码不起作用。 我见过使用“using”删除所有模板参数但没有保留一个的示例。这甚至可能吗?

【问题讨论】:

    标签: c++ c++11 templates typedef using


    【解决方案1】:

    尝试删除MatrixX之后的&lt;T&gt;

    template<typename T>
    using MatrixX<T> = Eigen::Matrix<T, ...
    // wrong ....^^^
    

    如果您在名称 foousing 定义之前使用模板声明,则暗示您是在 foo 上定义模板参数,所以很简单

    namespace EigenRM
    {
        template<typename T>
        using MatrixX = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
    }
    

    -- 编辑--

    OP 说

    这正是我不想做的。我正在尝试在未模板化的函数中编写 EigenRM::MatrixX&lt;double&gt; // 而不是 Eigen::Matrix&lt;double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor&gt;

    这正是删除“&lt;T&gt;”后得到的结果:EigenRM::MatrixX&lt;double&gt; 成为Eigen::Matrix&lt;double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor&gt; 的别名。

    我没有安装Eigen,但下面的例子应该能解释我的意思

    #include <type_traits>
    
    template <typename, typename, typename>
    struct foo;
    
    template <typename T>
    using bar = foo<T, float, int>;
    
    int main ()
     {
       static_assert(std::is_same< bar<double>,
                                   foo<double, float, int> >{}, "!");
     }
    

    【讨论】:

    • 这正是我不想做的。我正在尝试在未模板化的函数中编写 EigenRM::MatrixX //而不是 Eigen::Matrix
    • @coJetty - 回答改进:希望这会有所帮助。
    • 啊,我明白了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多