Skip to content
Snippets Groups Projects
Commit fddf919f authored by Benjamin Cumming's avatar Benjamin Cumming
Browse files

Modifications to util::optional for KNL compilation

The Intel compiler was unable to compile the
`util::optional` code, and also created some warnings
in the unit tests for optional
* rephrase `enable_if` template argument to use `::value`
  explicitly to work around Intel compiler bug.
* move helper types out of anonymous namespaces in the
  optional unit tests
    * fix warnings about unused member functions
* remove some white space in optional code
parent 3aac3d53
No related branches found
No related tags found
No related merge requests found
...@@ -332,7 +332,9 @@ struct optional<X&>: detail::optional_base<X&> { ...@@ -332,7 +332,9 @@ struct optional<X&>: detail::optional_base<X&> {
template <typename T> template <typename T>
optional(optional<T&>& ot): base(ot.set,ot.ref()) {} optional(optional<T&>& ot): base(ot.set,ot.ref()) {}
template <typename Y,typename = typename std::enable_if<!detail::is_optional<Y>()>::type> template <
typename Y,
typename = typename std::enable_if<!detail::is_optional<Y>::value>>
optional& operator=(Y& y) { optional& operator=(Y& y) {
set = true; set = true;
ref() = y; ref() = y;
...@@ -363,7 +365,7 @@ struct optional<void>: detail::optional_base<void> { ...@@ -363,7 +365,7 @@ struct optional<void>: detail::optional_base<void> {
template <typename T> template <typename T>
optional(const optional<T>& o): base(o.set,true) {} optional(const optional<T>& o): base(o.set,true) {}
template <typename T> template <typename T>
optional& operator=(T) { optional& operator=(T) {
set = true; set = true;
......
...@@ -53,7 +53,7 @@ TEST(optionalm,deref) { ...@@ -53,7 +53,7 @@ TEST(optionalm,deref) {
explicit foo(int a_): a(a_) {} explicit foo(int a_): a(a_) {}
double value() { return 3.0*a; } double value() { return 3.0*a; }
}; };
optional<foo> f=foo(2); optional<foo> f=foo(2);
EXPECT_EQ(6.0,f->value()); EXPECT_EQ(6.0,f->value());
EXPECT_EQ(2,(*f).a); EXPECT_EQ(2,(*f).a);
...@@ -88,22 +88,20 @@ TEST(optionalm,assign_returns) { ...@@ -88,22 +88,20 @@ TEST(optionalm,assign_returns) {
EXPECT_EQ(&a,bp); EXPECT_EQ(&a,bp);
} }
namespace { struct nomove {
struct nomove { int value;
int value;
nomove(): value(0) {} nomove(): value(0) {}
nomove(int i): value(i) {} nomove(int i): value(i) {}
nomove(const nomove &n): value(n.value) {} nomove(const nomove &n): value(n.value) {}
nomove(nomove &&n) = delete; nomove(nomove &&n) = delete;
nomove &operator=(const nomove &n) { value=n.value; return *this; } nomove &operator=(const nomove &n) { value=n.value; return *this; }
bool operator==(const nomove &them) const { return them.value==value; }
bool operator!=(const nomove &them) const { return !(*this==them); }
};
bool operator==(const nomove &them) const { return them.value==value; }
bool operator!=(const nomove &them) const { return !(*this==them); }
};
}
TEST(optionalm,ctor_nomove) { TEST(optionalm,ctor_nomove) {
optional<nomove> a(nomove(3)); optional<nomove> a(nomove(3));
EXPECT_EQ(nomove(3),a.get()); EXPECT_EQ(nomove(3),a.get());
...@@ -116,30 +114,28 @@ TEST(optionalm,ctor_nomove) { ...@@ -116,30 +114,28 @@ TEST(optionalm,ctor_nomove) {
EXPECT_EQ(nomove(4),b.get()); EXPECT_EQ(nomove(4),b.get());
} }
namespace { struct nocopy {
struct nocopy { int value;
int value;
nocopy(): value(0) {}
nocopy(): value(0) {} nocopy(int i): value(i) {}
nocopy(int i): value(i) {} nocopy(const nocopy &n) = delete;
nocopy(const nocopy &n) = delete; nocopy(nocopy &&n) {
nocopy(nocopy &&n) { value=n.value;
value=n.value; n.value=0;
n.value=0; }
}
nocopy &operator=(const nocopy &n) = delete;
nocopy &operator=(const nocopy &n) = delete; nocopy &operator=(nocopy &&n) {
nocopy &operator=(nocopy &&n) { value=n.value;
value=n.value; n.value=-1;
n.value=-1; return *this;
return *this; }
}
bool operator==(const nocopy &them) const { return them.value==value; }
bool operator==(const nocopy &them) const { return them.value==value; } bool operator!=(const nocopy &them) const { return !(*this==them); }
bool operator!=(const nocopy &them) const { return !(*this==them); } };
};
}
TEST(optionalm,ctor_nocopy) { TEST(optionalm,ctor_nocopy) {
optional<nocopy> a(nocopy(5)); optional<nocopy> a(nocopy(5));
EXPECT_EQ(nocopy(5),a.get()); EXPECT_EQ(nocopy(5),a.get());
...@@ -152,12 +148,10 @@ TEST(optionalm,ctor_nocopy) { ...@@ -152,12 +148,10 @@ TEST(optionalm,ctor_nocopy) {
EXPECT_EQ(nocopy(6),b.get()); EXPECT_EQ(nocopy(6),b.get());
} }
namespace { optional<double> odd_half(int n) {
optional<double> odd_half(int n) { optional<double> h;
optional<double> h; if (n%2==1) h=n/2.0;
if (n%2==1) h=n/2.0; return h;
return h;
}
} }
TEST(optionalm,bind) { TEST(optionalm,bind) {
...@@ -199,7 +193,7 @@ TEST(optionalm,void) { ...@@ -199,7 +193,7 @@ TEST(optionalm,void) {
TEST(optionalm,bind_to_void) { TEST(optionalm,bind_to_void) {
optional<int> a,b(3); optional<int> a,b(3);
int call_count=0; int call_count=0;
auto vf=[&call_count](int i) -> void { ++call_count; }; auto vf=[&call_count](int i) -> void { ++call_count; };
...@@ -213,12 +207,14 @@ TEST(optionalm,bind_to_void) { ...@@ -213,12 +207,14 @@ TEST(optionalm,bind_to_void) {
EXPECT_TRUE((bool)x); EXPECT_TRUE((bool)x);
EXPECT_EQ(1,call_count); EXPECT_EQ(1,call_count);
} }
TEST(optionalm,bind_to_optional_void) { TEST(optionalm,bind_to_optional_void) {
optional<int> a,b(3),c(4); optional<int> a,b(3),c(4);
int count=0; int count=0;
auto count_if_odd=[&count](int i) { return i%2?(++count,optional<void>(true)):optional<void>(); }; auto count_if_odd=[&count](int i) {
return i%2?(++count,optional<void>(true)):optional<void>();
};
auto x=a >> count_if_odd; auto x=a >> count_if_odd;
EXPECT_EQ(typeid(optional<void>),typeid(x)); EXPECT_EQ(typeid(optional<void>),typeid(x));
...@@ -242,12 +238,10 @@ TEST(optionalm,bind_with_ref) { ...@@ -242,12 +238,10 @@ TEST(optionalm,bind_with_ref) {
EXPECT_EQ(11,*a); EXPECT_EQ(11,*a);
} }
namespace { struct check_cref {
struct check_cref { int operator()(const int &) { return 10; }
int operator()(const int &) { return 10; } int operator()(int &) { return 11; }
int operator()(int &) { return 11; } };
};
}
TEST(optionalm,bind_constness) { TEST(optionalm,bind_constness) {
check_cref checker; check_cref checker;
...@@ -314,7 +308,7 @@ TEST(optionalm,and_operator) { ...@@ -314,7 +308,7 @@ TEST(optionalm,and_operator) {
auto zb=false & b; auto zb=false & b;
EXPECT_EQ(typeid(zb),typeid(b)); EXPECT_EQ(typeid(zb),typeid(b));
EXPECT_FALSE((bool)zb); EXPECT_FALSE((bool)zb);
auto b3=b & 3; auto b3=b & 3;
EXPECT_EQ(typeid(b3),typeid(optional<int>)); EXPECT_EQ(typeid(b3),typeid(optional<int>));
EXPECT_TRUE((bool)b3); EXPECT_TRUE((bool)b3);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment