我正在尝试根据整数数组创建简单 LookUpTable ,其中的想法是在编译时计算
尝试将其用于我可能拥有的各种整数类型的任何其他未来表,我需要它作为模板。
所以我有一个 LookUpTable.h
func test() -> Void {
var data: String = "Hello World"
data = md5(data)
data = base64Encode(data)
print("data = \(data)") //YjEwYThkYjE2NGUwNzU0MTA1YjdhOTliZTcyZTNmZTU=
}
我正试图在课堂上使用它来快速衰减整数距离的整数信号。
例如。这只是 Foo.h
的示例用法#ifndef LOOKUPTABLE_H
#define LOOKUPTABLE_H
#include <stdexcept> // out_of_range
template <typename T, std::size_t NUMBER_OF_ELEMENTS>
class LookUpTableIndexed
{
private:
//constexpr static std::size_t NUMBER_OF_ELEMENTS = N;
// LookUpTable
T m_lut[ NUMBER_OF_ELEMENTS ] {}; // ESSENTIAL T Default Constructor for COMPILE-TIME INTERPRETER!
public:
// Construct and Populate the LookUpTable such that;
// INDICES of values are MAPPED to the DATA values stored
constexpr LookUpTableIndexed() : m_lut {}
{
//ctor
}
// Returns the number of values stored
constexpr std::size_t size() const {return NUMBER_OF_ELEMENTS;}
// Returns the DATA value at the given INDEX
constexpr T& operator[](std::size_t n)
{
if (n < NUMBER_OF_ELEMENTS)
return m_lut[n];
else throw std::out_of_range("LookUpTableIndexed[] : OutOfRange!");
}
constexpr const T& operator[](std::size_t n) const
{
if (n < NUMBER_OF_ELEMENTS)
return m_lut[n];
else throw std::out_of_range("LookUpTableIndexed[] const : OutOfRange!");
}
using iterator = T*;
// Returns beginning and end of LookUpTable
constexpr iterator begin() {return &m_lut[0 ];}
constexpr iterator end () {return &m_lut[NUMBER_OF_ELEMENTS];}
};
#endif // LOOKUPTABLE_H
我尝试过几种方式,使用 CppCon 2015的youtube视频教程:Scott Schurr“constexpr:Applications”和其他人,但它不会编译给出错误;
#ifndef FOO_H
#define FOO_H
#include <limits> // max, digits
#include <stdlib.h> // abs
#include "LookUpTable.h" // LookUpTableIndexed
class Foo
{
private:
template <typename TDistance,
TDistance MAXIMUM_DISTANCE,
std::size_t NUMBER_OF_DIGITS>
struct DistanceAttenuation
{
private:
// Maximum value that can be held in this type
//constexpr auto MAXIMUM_DISTANCE = std::numeric_limits<TDistance>::max();
// Number of bits used by this type
//constexpr auto NUMBER_OF_DIGITS = std::numeric_limits<TDistance>::digits;
// LookUpTable
LookUpTableIndexed<TDistance, NUMBER_OF_DIGITS> m_attenuationRangeUpperLimit {}; // ESSENTIAL LookUpTable Default Constructor for COMPILE-TIME INTERPRETER!
// Returns the number of bits to BIT-SHIFT-RIGHT, attenuate, some signal
// given its distance from source
constexpr std::size_t attenuateBy(const TDistance distance)
{
for (std::size_t i {NUMBER_OF_DIGITS}; (i > 0); --i)
{
// While distance exceeds upper-limit, keep trying values
if (distance >= m_attenuationRangeUpperLimit[i - 1])
{
// Found RANGE the given distance occupies
return (i - 1);
}
}
throw std::logic_error("DistanceAttenuation::attenuateBy(Cannot attenuate signal using given distance!)");
}
public:
// Calculate the distance correction factors for signals
// so they can be attenuated to emulate the the effects of distance on signal strength
// ...USING THE INVERSE SQUARE RELATIONSHIP OF DISTANCE TO SIGNAL STRENGTH
constexpr DistanceAttenuation() : m_attenuationRangeUpperLimit {}
{
//ctor
// Populate the LookUpTable
for (std::size_t i {0}; (i < NUMBER_OF_DIGITS); ++i)
{
TDistance goo = 0; // Not an attenuation calculation
TDistance hoo = 0; // **FOR TEST ONLY!**
m_attenuationRangeUpperLimit[i] = MAXIMUM_DISTANCE - goo - hoo;
}
static_assert((m_attenuationRangeUpperLimit[0] == MAXIMUM_DISTANCE),
"DistanceAttenuation : Failed to Build LUT!");
}
// Attenuate the signal, s, by the effect of the distance
// by some factor, a, where;
// Positive contribution values are attenuated DOWN toward ZERO
// Negative UP ZERO
constexpr signed int attenuateSignal(const signed int s, const int a)
{
return (s < 0)? -(abs(s) >> a) :
(abs(s) >> a);
}
constexpr signed int attenuateSignalByDistance(const signed int s, const TDistance d)
{
return attenuateSignal(s, attenuateBy(d));
}
};
using SDistance_t = unsigned int;
constexpr static auto m_distanceAttenuation = DistanceAttenuation<SDistance_t,
std::numeric_limits<SDistance_t>::max(),
std::numeric_limits<SDistance_t>::digits>();
public:
Foo() {}
~Foo() {}
// Do some integer foo
signed int attenuateFoo(signed int signal, SDistance_t distance) {return m_distanceAttenuation::attenuateSignalByDistance(signal, distance);}
};
#endif // FOO_H
并且静态断言失败并带有
error: 'constexpr static auto m_distanceAttenuation...' used before its definition
表示它没有在编译时计算任何东西。
我是C ++的新手。
我知道我正在做一些明显的事情,但我不知道它是什么。
我是否误用静态或 constexpr ?
numeric_limits 是constexpr?
我做错了什么? 谢谢。
答案 0 :(得分:0)
一些观察
1)as observed by michalsrb,Foo
在您初始化m_distanceAttenuation
并且DistanceAttenuation
是Foo
的一部分时尚未完成,因此不完整。< / p>
很遗憾,您无法使用不完整类型初始化static constexpr
成员(jogojapan in this answer更好地解释了这一点)。
建议:在DistanceAttenuation
之外(及之前)定义Foo
;所以它是一个完整的类型,可用于初始化m_distanceAttenuation
;
template <typename TDistance,
TDistance MAXIMUM_DISTANCE,
std::size_t NUMBER_OF_DIGITS>
struct DistanceAttenuation
{
// ...
};
class Foo
{
// ...
};
2)在C ++ 14中,constexpr
方法不是const
方法;建议:将以下方法定义为const
,或者您无法在某些constexpr
表达式中使用它们
constexpr std::size_t attenuateBy (const TDistance distance) const
constexpr signed int attenuateSignal(const signed int s, const int a) const
constexpr signed int attenuateSignalByDistance(const signed int s, const TDistance d) const
3)在attenuateBy()
中,以下for
中的测试是真的
for (std::size_t i {NUMBER_OF_DIGITS - 1}; (i >= 0); --i)
因为std::size_t
永远是>= 0
,所以for
进入循环而永不退出;建议:将i
重新定义为int
或long
4)在attenuateFoo()
中,您使用m_DistanceAttenuation
,其中变量定义为m_distanceAttenuation
;建议:使用的变量的正确名称
5)在attenuateFoo()
中,您使用attenuateSignalByDistance()
运算符调用方法::
;建议:使用.
运算符,所以(考虑点(4))
signed int attenuateFoo(signed int signal, SDistance_t distance)
{return m_distanceAttenuation.attenuateSignalByDistance(signal, distance);}