条件类型特征通用引用的问题
Problems with conditional type-trait Universal Reference
因此,我有一个函数用于检查值,如果值无效,则抛出异常,否则在收到值时将其返回。我试图用普遍的虔诚和类型特征来概括这种惯例。我觉得我的例子在某些情况下有效,但不是全部。它似乎只适用于右值。
#include <iostream>
#include <utility>
#include <type_traits>
#include <string>
#include <vector>
using namespace std;
struct error_type{};
template <class T>
class has_empty
{
template<class U, class = typename std::enable_if<std::is_member_pointer<decltype(&U::empty)>::value>::type>
static std::true_type check(int);
template <class>
static std::false_type check(...);
public:
static constexpr bool value = decltype(check<T>(0))::value;
};
template <bool invalid>
struct valid {};
template <>
struct valid<false>
{
template<typename U, typename E>
static inline U&& check(U&& toCheck, const E& toThrow)
{
if (!toCheck)
std::cout << "NoEmpty Throw" << 'n';
else
std::cout << "NoEmpty valid" << 'n';
return std::forward<U>(toCheck);
}
};
template <>
struct valid<true>
{
template<typename U, typename E>
static inline U&& check(U&& toCheck, const E& toThrow)
{
if (toCheck.empty())
std::cout << "HasEmpty Throw" << 'n';
else
std::cout << "HasEmpty valid" << 'n';
return std::forward<U>(toCheck);
}
};
template<typename T
, typename E
, typename = typename std::enable_if<std::is_base_of<error_type, E>::value>::type>
inline T&& do_check(T&& toCheck, const E& toThrow)
{
return valid<has_empty<T>::value>::check(std::forward<T>(toCheck), toThrow);
}
struct HasEmpty
{
bool empty() {return false;}
};
struct NoEmpty
{
};
int main()
{
error_type e;
cout << has_empty<std::wstring>::value << 'n';
cout << has_empty<std::vector<std::wstring>>::value << 'n';
cout << has_empty<int>::value << 'n';
cout << has_empty<HasEmpty>::value << 'n';
cout << has_empty<NoEmpty>::value << 'n';
do_check(true, e);
do_check(false, e);
do_check(std::string("45"), e);
do_check(HasEmpty(), e);
do_check(std::vector<bool>(), e);
HasEmpty he;
do_check(std::move(he), e);
//do_check(he, e); // does not work, has_empty<T>::value is 0
}
生成输出
1
1
0
1
0
NoEmpty valid
NoEmpty Throw
HasEmpty valid
HasEmpty valid
HasEmpty Throw
HasEmpty valid
如果我取消注释最后一行,我会得到以下错误:
prog.cpp: In instantiation of 'static T&& valid<false, T, E>::check(T&&, const E&) [with T = HasEmpty&; E = error_type]':
prog.cpp:56:84: required from 'T&& do_check(T&&, const E&) [with T = HasEmpty&; E = error_type; <template-parameter-1-3> = void]'
prog.cpp:87:19: required from here
prog.cpp:30:11: error: no match for 'operator!' (operand type is 'HasEmpty')
if (!toCheck)
^
prog.cpp:30:11: note: candidate is:
prog.cpp:30:11: note: operator!(bool) <built-in>
prog.cpp:30:11: note: no known conversion for argument 1 from 'HasEmpty' to 'bool'
这表明CCD_ 1正在对CCD_ 2求值。我相信我可以做一些不同的工作来实现这一点,所以在这一点上,这是一种学术性的。尽管如此,任何帮助都将不胜感激。
当您将左值传递给do_check
时,它会在您的示例中将T
推导为HasEmpty&
。当然,引用类型没有名为empty
的成员函数,并且表达式decltype(&U::empty)
格式不正确,导致static std::true_type check(int)
重载导致SFINAE'd out。
如果更换
static constexpr bool value = decltype(check<T>(0))::value;
带有
static constexpr bool value = decltype(check<typename std::remove_reference<T>::type>(0))::value;
因此U
永远不是引用类型,您的代码将按预期工作。
实时演示
相关文章:
- C++ - 对 PCM::getInstance() 的未定义引用问题
- 通过键入的引用问题传递函数
- printf(时差程序)中的引用问题
- 在一个简单的程序中奇怪的通过引用问题传递
- 在实现OpenSceneGraph时在Qt中获取引用问题
- antlr4 c++目标的未定义引用问题
- 基本指针/引用问题
- 通过引用问题传递C++
- C++ OpenMP 任务 - 通过引用问题传递
- C/C++ 双指针取消引用问题
- Q字符串传递引用问题
- Q_OBJECT导致"未定义对v_table引用"问题
- 具有最小"extern"使用测试用例的未定义引用问题
- 下标操作符重载:返回引用问题
- LuaJIT FFI中的指针和引用问题
- c++和Netbeans的未定义引用问题
- 指针和引用问题
- void*指针中的引用问题
- Crypto++ Code::blocks未定义引用问题
- 使用 FFTW 的"未定义引用"问题