根据模板参数选择合适的复制构造函数

时间:2015-10-27 08:52:57

标签: c++ templates c++11 sfinae

我想根据模板参数值选择成员函数(复制构造函数)的实现。我想有两种方法:SFINAE和模板部分特化。

最后一个看起来像这样:

#include <iostream>

template<typename A, bool sw>
struct B
{
    B() {}
    B(const B &b);
};

template<typename A>
B<A, false>::B(const B<A, false> &b)
{
    std::cout << "false\n";
}

template<typename A>
B<A, true>::B(const B<A, true> &b)
{
    std::cout << "true\n";
}

int main()
{
}

它不编译:{{1​​} }。

SFINAE方法也失败了:

nested name specifier 'B<A, false>::' for declaration does not refer into a class, class template or class template partial specialization

这里的编译错误是#include <type_traits> #include <iostream> template<typename A, bool sw> struct B { B() {} template<typename U = typename std::enable_if<sw, B>::type> B(const U &b) { std::cout << "true\n"; } template<typename U = typename std::enable_if<!sw, B>::type> B(const U &b) { std::cout << "false\n"; } }; int main() { { B<int, true> b; auto bc = b; // cout << true } { B<int, false> b; auto bc = b; // cout << false } } constructor cannot be redeclared

有没有办法解决问题或以其他方式根据模板参数选择合适的复制构造函数?

3 个答案:

答案 0 :(得分:4)

template <typename A, bool sw>
struct B
{
    B() = default;

    B(const B& b) : B(b, std::integral_constant<bool, sw>{}) {}

private:
    B(const B& b, std::true_type)
    {
        std::cout << "true\n";
    }

    B(const B& b, std::false_type)
    {
        std::cout << "false\n";
    }
};

DEMO

答案 1 :(得分:1)

正如@Joachim Pileborg在评论中所说,你不能只专注一个班级的一个成员,你必须专注于整个班级。因此,您的部分专业化将如下所示:

template<typename A, bool sw>
struct B
{
    B() {}
    B(const B &b);
};

//Specialize when sw is false
template<typename A>
struct B<A, false>
{
     //The constructor in the specialized struct print false
     B(const B &b)
     {
        std::cout << "false\n";
     }
};

//Specialize when sw is true
template<typename A>
struct B<A, true>
{
    //The constructor in the specialized struct print true
    B(const B &b)
    {
        std::cout << "true\n";
    }
};

您可以获得SFINAE的最佳解决方案由@Piotr Skotnicki提供

答案 2 :(得分:0)

我知道这是一个很小的例子,但是

的错误
==> D:\bat\SO\33361955.bat
Unicode cp852 cp1250  comment
0x00C7   0x80   0xC7  LATIN CAPITAL LETTER C WITH CEDILLA
0x00FC   0x81   0xFC  LATIN SMALL LETTER U WITH DIAERESIS
0x00E9   0x82   0xE9  LATIN SMALL LETTER E WITH ACUTE
0x00E2   0x83   0xE2  LATIN SMALL LETTER A WITH CIRCUMFLEX
0x00E4   0x84   0xE4  LATIN SMALL LETTER A WITH DIAERESIS
0x016F   0x85   0xF9  LATIN SMALL LETTER U WITH RING ABOVE
0x0107   0x86   0xE6  LATIN SMALL LETTER C WITH ACUTE
0x00E7   0x87   0xE7  LATIN SMALL LETTER C WITH CEDILLA
0x0142   0x88   0xB3  LATIN SMALL LETTER L WITH STROKE
0x00EB   0x89   0xEB  LATIN SMALL LETTER E WITH DIAERESIS
0x0150   0x8a   0xD5  LATIN CAPITAL LETTER O WITH DOUBLE ACUTE
0x0151   0x8b   0xF5  LATIN SMALL LETTER O WITH DOUBLE ACUTE
0x00EE   0x8c   0xEE  LATIN SMALL LETTER I WITH CIRCUMFLEX
0x0179   0x8d   0x8F  LATIN CAPITAL LETTER Z WITH ACUTE
0x00C4   0x8e   0xC4  LATIN CAPITAL LETTER A WITH DIAERESIS
0x0106   0x8f   0xC6  LATIN CAPITAL LETTER C WITH ACUTE
0x00C9   0x90   0xC9  LATIN CAPITAL LETTER E WITH ACUTE
0x0139   0x91   0xC5  LATIN CAPITAL LETTER L WITH ACUTE
0x013A   0x92   0xE5  LATIN SMALL LETTER L WITH ACUTE
0x00F4   0x93   0xF4  LATIN SMALL LETTER O WITH CIRCUMFLEX
0x00F6   0x94   0xF6  LATIN SMALL LETTER O WITH DIAERESIS
0x013D   0x95   0xBC  LATIN CAPITAL LETTER L WITH CARON
0x013E   0x96   0xBE  LATIN SMALL LETTER L WITH CARON
0x015A   0x97   0x8C  LATIN CAPITAL LETTER S WITH ACUTE
0x015B   0x98   0x9C  LATIN SMALL LETTER S WITH ACUTE
0x00D6   0x99   0xD6  LATIN CAPITAL LETTER O WITH DIAERESIS
0x00DC   0x9a   0xDC  LATIN CAPITAL LETTER U WITH DIAERESIS
0x0164   0x9b   0x8D  LATIN CAPITAL LETTER T WITH CARON
0x0165   0x9c   0x9D  LATIN SMALL LETTER T WITH CARON
0x0141   0x9d   0xA3  LATIN CAPITAL LETTER L WITH STROKE
0x00D7   0x9e   0xD7  MULTIPLICATION SIGN
0x010D   0x9f   0xE8  LATIN SMALL LETTER C WITH CARON
0x00E1   0xa0   0xE1  LATIN SMALL LETTER A WITH ACUTE
0x00ED   0xa1   0xED  LATIN SMALL LETTER I WITH ACUTE
0x00F3   0xa2   0xF3  LATIN SMALL LETTER O WITH ACUTE
0x00FA   0xa3   0xFA  LATIN SMALL LETTER U WITH ACUTE
0x0104   0xa4   0xA5  LATIN CAPITAL LETTER A WITH OGONEK
0x0105   0xa5   0xB9  LATIN SMALL LETTER A WITH OGONEK
0x017D   0xa6   0x8E  LATIN CAPITAL LETTER Z WITH CARON
0x017E   0xa7   0x9E  LATIN SMALL LETTER Z WITH CARON
0x0118   0xa8   0xCA  LATIN CAPITAL LETTER E WITH OGONEK
0x0119   0xa9   0xEA  LATIN SMALL LETTER E WITH OGONEK
0x00AC   0xaa   0xAC  NOT SIGN
0x017A   0xab   0x9F  LATIN SMALL LETTER Z WITH ACUTE
0x010C   0xac   0xC8  LATIN CAPITAL LETTER C WITH CARON
0x015F   0xad   0xBA  LATIN SMALL LETTER S WITH CEDILLA
0x00AB   0xae   0xAB  LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BB   0xaf   0xBB  RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00C1   0xb5   0xC1  LATIN CAPITAL LETTER A WITH ACUTE
0x00C2   0xb6   0xC2  LATIN CAPITAL LETTER A WITH CIRCUMFLEX
0x011A   0xb7   0xCC  LATIN CAPITAL LETTER E WITH CARON
0x015E   0xb8   0xAA  LATIN CAPITAL LETTER S WITH CEDILLA
0x017B   0xbd   0xAF  LATIN CAPITAL LETTER Z WITH DOT ABOVE
0x017C   0xbe   0xBF  LATIN SMALL LETTER Z WITH DOT ABOVE
0x0102   0xc6   0xC3  LATIN CAPITAL LETTER A WITH BREVE
0x0103   0xc7   0xE3  LATIN SMALL LETTER A WITH BREVE
0x00A4   0xcf   0xA4  CURRENCY SIGN
0x0111   0xd0   0xF0  LATIN SMALL LETTER D WITH STROKE
0x0110   0xd1   0xD0  LATIN CAPITAL LETTER D WITH STROKE
0x010E   0xd2   0xCF  LATIN CAPITAL LETTER D WITH CARON
0x00CB   0xd3   0xCB  LATIN CAPITAL LETTER E WITH DIAERESIS
0x010F   0xd4   0xEF  LATIN SMALL LETTER D WITH CARON
0x0147   0xd5   0xD2  LATIN CAPITAL LETTER N WITH CARON
0x00CD   0xd6   0xCD  LATIN CAPITAL LETTER I WITH ACUTE
0x00CE   0xd7   0xCE  LATIN CAPITAL LETTER I WITH CIRCUMFLEX
0x011B   0xd8   0xEC  LATIN SMALL LETTER E WITH CARON
0x0162   0xdd   0xDE  LATIN CAPITAL LETTER T WITH CEDILLA
0x016E   0xde   0xD9  LATIN CAPITAL LETTER U WITH RING ABOVE
0x00D3   0xe0   0xD3  LATIN CAPITAL LETTER O WITH ACUTE
0x00DF   0xe1   0xDF  LATIN SMALL LETTER SHARP S
0x00D4   0xe2   0xD4  LATIN CAPITAL LETTER O WITH CIRCUMFLEX
0x0143   0xe3   0xD1  LATIN CAPITAL LETTER N WITH ACUTE
0x0144   0xe4   0xF1  LATIN SMALL LETTER N WITH ACUTE
0x0148   0xe5   0xF2  LATIN SMALL LETTER N WITH CARON
0x0160   0xe6   0x8A  LATIN CAPITAL LETTER S WITH CARON
0x0161   0xe7   0x9A  LATIN SMALL LETTER S WITH CARON
0x0154   0xe8   0xC0  LATIN CAPITAL LETTER R WITH ACUTE
0x00DA   0xe9   0xDA  LATIN CAPITAL LETTER U WITH ACUTE
0x0155   0xea   0xE0  LATIN SMALL LETTER R WITH ACUTE
0x0170   0xeb   0xDB  LATIN CAPITAL LETTER U WITH DOUBLE ACUTE
0x00FD   0xec   0xFD  LATIN SMALL LETTER Y WITH ACUTE
0x00DD   0xed   0xDD  LATIN CAPITAL LETTER Y WITH ACUTE
0x0163   0xee   0xFE  LATIN SMALL LETTER T WITH CEDILLA
0x00B4   0xef   0xB4  ACUTE ACCENT
0x00AD   0xf0   0xAD  SOFT HYPHEN
0x02DD   0xf1   0xBD  DOUBLE ACUTE ACCENT
0x02DB   0xf2   0xB2  OGONEK
0x02C7   0xf3   0xA1  CARON
0x02D8   0xf4   0xA2  BREVE
0x00A7   0xf5   0xA7  SECTION SIGN
0x00F7   0xf6   0xF7  DIVISION SIGN
0x00B8   0xf7   0xB8  CEDILLA
0x00B0   0xf8   0xB0  DEGREE SIGN
0x00A8   0xf9   0xA8  DIAERESIS
0x02D9   0xfa   0xFF  DOT ABOVE
0x0158   0xfc   0xD8  LATIN CAPITAL LETTER R WITH CARON
0x0159   0xfd   0xF8  LATIN SMALL LETTER R WITH CARON
0x00A0   0xff   0xA0  NO-BREAK SPACE