1

私はこのQ/Aを読んでいましたが、回答のいくつかは、C++17 で Fold Expressions を使用するための可能な解決策を提供していました。自分のコード内でこの手法を試してみようと思いました。

これは私が試したことです:

一部のヘッダー

#pragma once

#include <bitset>
#include <type_traits>

using Bit = std::bitset<1>;

template<typename... Bits>
typename std::enable_if<(std::is_same<Bits, Bit>::value && ...), Bit>::type
And(Bits... bits) {
    return (bits&...);
}

main.cpp

#include <iostream>
#include "Some Header"

int main()
{   
    Bit b1_0{0};
    Bit b1_1{1};
    Bit b2_0{0};
    Bit b2_1{1};

    // Intended uses:
    Bit res1 = And(b1_0, b1_1, b2_0); // res1 should = 0
    Bit res2 = And(b1_1, b2_1); // res2 should = 1

    std::cout << "res1 = " << res1.to_string() << '\n';
    std::cout << "res2 = " << res2.to_string() << '\n';

    return 0;
}

Visual Studio 2017 を使用しています。これはコンパイルに失敗します"fatal error C1001"

それがfold式から来ているのか、それとも関数などに渡される&それぞれに繰り返されるものをどのように適用しようとしているのかわからない.Bit

Compiler Explorer を使用してこれを試すと: goldbot GCC または Clang を使用して正常にコンパイルされますが、Visual Studio では失敗します...

Visual Studio で機能するこれと同様のことをどのように行うことができますか?

4

1 に答える 1