我试图为非字符数组部分特化一个特征:
template<typename T>
struct is_container : std::false_type {};
template<typename T, unsigned N>
struct is_container<T[N]>
: std::enable_if<!std::is_same<T, char>::value, std::true_type>::type {};
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010给了我一个C2039(type不是enable_if......的元素).但是,SFINAE不应该只是在这里触底而不是给出编译错误吗?或者SFINAE在这种情况下不适用?
当然,我可以将非char和char的特化分开:
template<typename T>
struct is_container : std::false_type {};
template<typename T, unsigned N>
struct is_container<T[N]> : std::true_type {};
template<unsigned N>
struct is_container<char[N]> : std::false_type {};
Run Code Online (Sandbox Code Playgroud)
但我真的想知道为什么SFINAE在这种特殊情况下不起作用.
我如何在Haskell中编写如下内容:
showSquare :: (Show a, Num a) => a -> String
showSquare x = "The square of " ++ (show x) ++ " is " ++ (show (x * x))
showSquare :: (Show a, not Num a) => a -> String
showSquare x = "I don't know how to square " ++ (show x)
Run Code Online (Sandbox Code Playgroud)
基本上,类似于C++中的boost :: enable_if.
GHC扩展是可以的.
我有以下不编译的代码.这是接受参数的模板类中的两个函数
typename std::enable_if<std::is_void<Ret>::value, Ret>::type _on_dispatched() {
// ...
}
typename std::enable_if<!std::is_void<Ret>::value, Ret>::type _on_dispatched() {
// ....
}
Run Code Online (Sandbox Code Playgroud)
我希望根据Ret的类型对成员方法进行专门化.
有人有点想法吗?
基本上我希望我的范围类型可以隐式转换Range<const char>为Range<const unsigned char>.std :: enable_if似乎不可能,因为该函数不带参数且没有返回.是什么工作?
这基本上是我尝试过的:
template<typename T>
class Range{
T* begin_;
T* end_;
public:
Range(T* begin,T* end):begin_{begin},end_{end}{}
template<int N>
Range(T (&a)[N]):begin_{static_cast<T*>(&a[0])},end_{static_cast<T*>(&a[N-1])}{}
T* Begin(){return begin_;}
T* End(){return end_;}
operator typename std::enable_if<std::is_same<T,const char>::value,Range<const unsigned char>&>::Type (){
return *reinterpret_cast<Range<const unsigned char>*>(this);
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个模板类,其类型是迭代器.我想根据模板参数的iterator_category启用/禁用特定成员函数.特别是,operator--如果模板参数是双向迭代器,我想启用.我的尝试是这样的:
typename std::enable_if<
std::is_base_of<std::bidirectional_iterator_tag,
MyTemplateParameter>::value,
MyType&>::type
operator --() {
//do work
return *this;
}
Run Code Online (Sandbox Code Playgroud)
Clang告诉我(粗略地): error: no type named 'type' in 'std::__1::enable_if<false, MyTemplateParameter>'; 'enable_if' cannot be used to disable this declaration
有没有办法完成我正在尝试的东西?
以下是某些上下文中的示例:
#include <iterator>
#include <type_traits>
template <typename TagType>
class test {
public:
typename std::enable_if<
std::is_base_of<std::bidirectional_iterator_tag,
TagType>::value,
test>::type
operator --() {
return *this;
}
};
int main(){
test<std::random_access_iterator_tag> t1;
test<std::forward_iterator_tag> t2;
/*
breakTemps.cpp:13:2: error: no type named 'type' in 'std::__1::enable_if<false, test<std::__1::forward_iterator_tag> >'; 'enable_if' cannot be used to …Run Code Online (Sandbox Code Playgroud) 我正在尝试定义一个has_ostream_operator<T>SFINAE测试,以检查我是否可以cout一个给定的类型.我有它工作,但只有在我的定义中has_ostream_operator我称之为operator<<方法而不是作为中缀运算符.换句话说,这有效:
decltype(std::declval<std::ostream>().operator<<(std::declval<T>()))>
这不是:
decltype(std::declval<std::ostream>() << std::declval<T>())>
下面的测试用例(也可以参见http://coliru.stacked-crooked.com/a/d257d9d6e0f3f6d9).请注意,我包含了void_t的定义,因为我只在C++ 14上.
#include <iostream>
namespace std {
template<class...>
using void_t = void;
}
template<class, class = std::void_t<>>
struct has_ostream_operator : std::false_type {};
template<class T>
struct has_ostream_operator<
T,
std::void_t<
decltype(
std::declval<std::ostream>().operator<<(std::declval<T>()))>>
: std::true_type {};
struct Foo {};
template<class X>
void print(
const X& x,
std::enable_if_t<has_ostream_operator<X>::value>* = 0)
{
std::cout << x;
}
template<class X>
void print(
const X&,
std::enable_if_t<!has_ostream_operator<X>::value>* = 0)
{
std::cout << "(no ostream operator<< …Run Code Online (Sandbox Code Playgroud) template <bool Cond, typename Type = void>
using Enable_if = typename std::enable_if<Cond, Type>::type;
class Degree;
template <typename T>
constexpr inline bool Is_Degree() {
return std::is_base_of<Degree, T>::value;
}
class Degree {
public:
std::size_t inDeg = 0;
};
template <typename Satellite = Degree>
class Vertex: public Satellite {
public:
explicit Vertex(int num): n(num) {}
private:
std::size_t n;
};
template <typename Satellite = Degree>
class Edge {
public:
// i want have different constructor depending on
// whether Vertex is (directly or indirectly) …Run Code Online (Sandbox Code Playgroud) 只是一个简短的问题给出一个函数,我想返回一个基础类型enum class:
为什么这个版本工作正常
template<typename T>
constexpr inline
typename std::enable_if_t<
std::is_enum<T>::value,
typename std::underlying_type_t<T>
>
enumValue(T p_rVal) noexcept
{
return static_cast<typename std::underlying_type_t<T>>(p_rVal);
}
if (enumValue(myEnumClass) == 0) {}
Run Code Online (Sandbox Code Playgroud)
而这一个失败了" 找不到匹配的重载函数 "(VS 2015)错误:
template<
typename T,
typename std::enable_if_t<
std::is_enum<T>::value,
typename std::underlying_type_t<T>
>
>
constexpr inline
typename std::underlying_type_t<T>
enumValue(T p_rVal) noexcept
{
return static_cast<typename std::underlying_type_t<T>>(p_rVal);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!
我试图std::enable_if在C++ 11中更好地理解并且一直在尝试编写一个最小的例子:一个A具有成员函数的类,void foo()它具有基于T类模板中的类型的不同实现.
下面的代码给出了期望的结果,但我还没有完全理解它.为什么版本V2有效,但不是V1?为什么U需要"冗余"类型?
#include <iostream>
#include <type_traits>
template <typename T>
class A {
public:
A(T x) : a_(x) {}
// Enable this function if T == int
/* V1 */ // template < typename std::enable_if<std::is_same<T,int>::value,int>::type = 0>
/* V2 */ template <typename U=T, typename std::enable_if<std::is_same<U,int>::value,int>::type = 0>
void foo() { std::cout << "\nINT: " << a_ << "\n"; }
// Enable this function if T …Run Code Online (Sandbox Code Playgroud) 我第一次尝试std :: enable_if并且正在努力.任何指导将不胜感激.
作为一个玩具示例,这里是一个简单的静态向量类,我想为其定义一个复制构造函数,但行为取决于向量的相对大小:
所以vector类是:
template <size_t _Size>
class Vector
{
double _data[_Size];
public:
Vector()
{
std::fill(_data, _data + _Size, 0.0);
}
const double* data() const
{
return _data;
}
...
};
Run Code Online (Sandbox Code Playgroud)
复制构造函数应该支持这样的东西,将v3的前2个元素复制到v2中:
Vector<3> v3;
Vector<2> v2(v3);
Run Code Online (Sandbox Code Playgroud)
我尝试了一个行为的复制构造函数1.像这样编译:
template <size_t _OtherSize,
typename = typename std::enable_if_t<_Size <= _OtherSize>>
Vector(const Vector<_OtherSize>& v) : Vector()
{
std::copy(v.data(), v.data() + _Size, _data);
}
Run Code Online (Sandbox Code Playgroud)
但是编译器无法将其与行为2区分开来.即使enable_if条件是互斥的.
template <size_t _OtherSize,
typename = typename std::enable_if_t<_OtherSize < _Size>>
Vector(const Vector<_OtherSize>& v) : Vector()
{
std::copy(v.data(), v.data() + …Run Code Online (Sandbox Code Playgroud)