Skip to content

Commit c09f6d8

Browse files
committed
use C++11 constexpr to replace static typeCode
clang-cl has a problem with static memeber variable llvm/llvm-project#54814. But it has no problem with constexpr member variable.
1 parent c16f19c commit c09f6d8

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/factory/PVDataCreateFactory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using std::min;
3030

3131
namespace epics { namespace pvData {
3232

33-
33+
#if __cplusplus < 201103L
3434
template<> const ScalarType PVBoolean::typeCode = pvBoolean;
3535
template<> const ScalarType PVByte::typeCode = pvByte;
3636
template<> const ScalarType PVShort::typeCode = pvShort;
@@ -56,7 +56,7 @@ template<> const ScalarType PVULongArray::typeCode = pvULong;
5656
template<> const ScalarType PVFloatArray::typeCode = pvFloat;
5757
template<> const ScalarType PVDoubleArray::typeCode = pvDouble;
5858
template<> const ScalarType PVStringArray::typeCode = pvString;
59-
59+
#endif
6060

6161
template<typename T>
6262
PVScalarValue<T>::~PVScalarValue() {}

src/pv/pvData.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <iterator>
1717
#include <iostream>
1818
#include <iomanip>
19+
#if __cplusplus >= 201103L
20+
#include <type_traits>
21+
#endif
1922

2023
#include <epicsAssert.h>
2124

@@ -124,6 +127,36 @@ typedef std::tr1::shared_ptr<PVUnionArrayPtrArray> PVUnionArrayPtrArrayPtr;
124127
class PVDataCreate;
125128
typedef std::tr1::shared_ptr<PVDataCreate> PVDataCreatePtr;
126129

130+
#if __cplusplus >= 201103L
131+
template <typename T>
132+
constexpr ScalarType typeToCode() {
133+
if (std::is_same<T, boolean>::value)
134+
return pvBoolean;
135+
else if (std::is_same<T, int8>::value)
136+
return pvByte;
137+
else if (std::is_same<T, uint8>::value)
138+
return pvUByte;
139+
else if (std::is_same<T, int16>::value)
140+
return pvShort;
141+
else if (std::is_same<T, uint16>::value)
142+
return pvUShort;
143+
else if (std::is_same<T, int32>::value)
144+
return pvInt;
145+
else if (std::is_same<T, uint32>::value)
146+
return pvUInt;
147+
else if (std::is_same<T, int64>::value)
148+
return pvLong;
149+
else if (std::is_same<T, uint64>::value)
150+
return pvULong;
151+
else if (std::is_same<T, float>::value)
152+
return pvFloat;
153+
else if (std::is_same<T, double>::value)
154+
return pvDouble;
155+
else if (std::is_same<T, std::string>::value)
156+
return pvString;
157+
}
158+
#endif
159+
127160
/**
128161
* @brief This class is implemented by code that calls setPostHander
129162
*
@@ -383,7 +416,11 @@ class epicsShareClass PVScalarValue : public PVScalar {
383416
typedef T* pointer;
384417
typedef const T* const_pointer;
385418

419+
#if __cplusplus < 201103L
386420
static const ScalarType typeCode;
421+
#else
422+
constexpr static const ScalarType typeCode = typeToCode<T>();
423+
#endif
387424

388425
/**
389426
* Destructor
@@ -1184,8 +1221,11 @@ class epicsShareClass PVValueArray : public detail::PVVectorStorage<T,PVScalarAr
11841221
typedef ::epics::pvData::shared_vector<T> svector;
11851222
typedef ::epics::pvData::shared_vector<const T> const_svector;
11861223

1187-
1224+
#if __cplusplus < 201103L
11881225
static const ScalarType typeCode;
1226+
#else
1227+
constexpr static const ScalarType typeCode = typeToCode<T>();
1228+
#endif
11891229

11901230
/**
11911231
* Destructor

0 commit comments

Comments
 (0)