使用类型为T的std :: array从T构造可构造类型的std :: array

时间:2018-06-17 18:51:05

标签: c++ arrays stl

请考虑以下类型:

import requests
import random
import time

url = 'https://mrcnoir.com/account/login'
errState = False

while True:
    try:
        password_page = requests.get('{}'.format(url), timeout=5)
        password_page.raise_for_status()

    except requests.exceptions.RequestException as err:
        if not errState:            
            print('Error checking password page! - {}'.format(url) + ' - ' + str(err))
            print("SENDING...2")   # <-----------------
            errState = True
        continue

    else:
        # *************---If password up---**************

        if ('password' in password_page.text):
            # Password page is up
            print('Password page is up! - ' + '{}'.format(url))
            print("SENDING...1")
            errState = False


        #else:
        #    # Password page is down ???
        #    # this is not a useful test, if it is down you get an exception and
        #    # should handle it there
        #    print('Password page is down! - ' + '{}'.format(url))
        #    print("SENDING...2") #<---- If it comes in here - it will be stuck forever and just keep posting this print...

    # *************---Retry between 6-12 random.---**************
    finally:
        time.sleep(random.randint(6, 12))

现在我想使用struct Part { float data; }; struct Wrap { Wrap( const Part& p ) :data( p.data ) {} float data; }; 初始化std::array<Part, N>

std::array<Wrap, N>

(这会引发错误int main() { std::array<Part, 3> parts{ Part{ 1.0f }, Part{ 2.0f }, Part{ 3.0f } }; std::array<Wrap, 3> wrappers( parts ); return 0; }

如何使用"conversion from 'std::array<Part, 3>' to non-scalar type 'std::array<Wrap, 3>' requested"类型std::array来初始化T类型的std::array

2 个答案:

答案 0 :(得分:2)

您可以使用辅助函数自动转换:

// Convert each element based on a sequence of indices:
template<typename ToType, typename FromType, std::size_t... Indices>
std::array<ToType, sizeof...(Indices)>
convert_impl(const std::array<FromType, sizeof...(Indices)>& input, std::index_sequence<Indices...>)
{
    return {ToType(std::get<Indices>(input))...};
}

// Wraps convert_impl to hide the use of index_sequence from users:
template<typename ToType, typename FromType, std::size_t N>
std::array<ToType, N> convert(const std::array<FromType, N>& input)
{
    return convert_impl<ToType>(input, std::make_index_sequence<N>{});
}

int main()
{
    std::array<Part, 3> parts {Part{1.0f}, Part{2.0f}, Part{3.0f}};
    std::array<Wrap, 3> wraps = convert<Wrap>(parts);

    return 0;
}

答案 1 :(得分:1)

可能的简单选项是:

0。改为使用vector(数组仍然是邪恶的0_o)。

    std::vector<Part> ps{Part{1.0}, Part{2.0}, Part{3.0}};
    std::vector<Wrap> ws(ps.cbegin(), ps.cend());

予。说清楚。

    std::array<Part, 3> parts{ Part{ 1.0f }, Part{ 2.0f }, Part{ 3.0f } };
    std::array<Wrap, 3> wrappers = {parts[0], parts[1], parts[2]};

II。拆分构造和初始化。

struct Unwrap {
    Unwrap() {}
    Unwrap(Part const &p): data(p.data) {}
    float data;
};

int main() {
    std::array<Part, 3> parts{ Part{ 1.0f }, Part{ 2.0f }, Part{ 3.0f } };
    std::array<Unwrap, 3> unwrappers;
    std::copy(parts.cbegin(), parts.cend(), unwrappers.begin());
}

顺便说一下,Part初始化中带括号的代码是否甚至编译?我只能通过将它们改为括号来实现它。

相关问题