基于C ++ 11中的模板参数选择数组大小?

时间:2019-06-23 23:13:35

标签: c++ c++11 templates template-meta-programming

考虑以下代码:

def third_site_fc(third_site_franche_comte = requests.get("https://www.paruvendu.fr/a/utilitaire-occasion/tracteur-routier/")):

    third_soup_fc = BeautifulSoup(third_site_franche_comte.text,'html.parser')

    my_list = [] # Try to avoid reserved keywords
    for articles_third_site in third_soup_fc.find_all('div',class_='ergov3-txtannonce'):
        text = (articles_third_site.text
                  .replace('\n','')
                  .replace('\r',''))
        my_list.append(text)

    return my_list

print(third_site_fc())

我希望基于类型Sub FindAndGo() Dim x As String, r As Range With Application x = .InputBox(Prompt:="Enter search value", Type:=2) Set r = Range("A:A").Cells.Find(what:=x, after:=Range("A1")) .Goto r End With End Sub 的数组enum class EnumType { Type1, Type2 }; constexpr std::size_t the_length; template <EnumType T> int function() { std::array<uint8_t, the_length> x; //some code here that uses x } 的长度具有不同的值。例如,如果x可以采用2个值之一(TT),那么我希望Type1的值如果为Type2则为10,并且一个值如果the_length,则为20。可以在C ++ 11中完成吗?谢谢

3 个答案:

答案 0 :(得分:4)

好的老三元运算符怎么了?

template <EnumType T>
int function()
{
  std::array<SomeType, T == EnumType::Type1 ? 10u : 20u> x;
}

如果Ttypename,而不是某种类型的值,则只需更改测试

template <typename T>
int function()
{
  std::array<T, std::is_same<SomeType1, T>::value ? 10u : 20u> x;
}

答案 1 :(得分:2)

正如@templatetypedef所说,但是C ++ 11可以做的甚至更多:

#include <array>
#include <cstddef>

enum class EnumType { T1, T2 };

template<EnumType T>
struct my_array_traits;

template<>
struct my_array_traits<EnumType::T1> {
    using type = float;
    constexpr static std::size_t value = 5;
};

template<>
struct my_array_traits<EnumType::T2> {
    using type = double;
    constexpr static std::size_t value = 10;
};

template<EnumType T>
struct my_other_array_traits;

template<>
struct my_other_array_traits<EnumType::T1> {
    using type = short;
    constexpr static std::size_t value = 20;
};

template<>
struct my_other_array_traits<EnumType::T2> {
    using type = long;
    constexpr static std::size_t value = 40;
};


template <EnumType T, template<EnumType> class array_traits>
int function()
{
    std::array<typename array_traits<T>::type,
               array_traits<T>::value> x;
    //some code here that uses x 
    return 0;
}

int main() {
    function<EnumType::T1, my_array_traits>();
    function<EnumType::T2, my_array_traits>();
    function<EnumType::T1, my_other_array_traits>();
    function<EnumType::T2, my_other_array_traits>();
    return 0;
}

答案 2 :(得分:0)

当然可以。您只需要另一个间接级别。这是执行此操作的一种方法:

/* array_for<T>::type gives you your array type. */
template <EnumType T> struct array_for;

/* Specialize to give different values based on what T is. */
template <> struct array_for<Option1> {
    using type = std::array<WhateverTypeYouWantToUse1, 10>;
};
template <> struct array_for<Option2> {
    using type = std::array<WhateverTypeYouWantToUse2, 20>;
};

template <EnumType T>
int function()
{
  typename array_for<T>::type myArray;
  /* do whatever coding dance with myArray seems most fitting. */
}

在这里,array_for助手struct模板以EnumType作为输入,然后根据发现的内容输出不同类型的数组。