我试图创建一组具有不同操作的策略类,这些操作实现为静态成员lambdas.这样我就可以根据给定的策略在相同操作的不同版本之间进行选择,并将操作传递给某些算法,例如std :: transform.
这引导我使用CRTP进行以下设计,其中基类实现了lambda.请注意,C++ 17允许定义constexpr lambdas.
namespace test
{
template <class B>
struct A
{
// The same thing happens if this is static const instead
static constexpr auto t = [](const auto c) {};
};
struct C : A<C>
{};
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我得到了错误
错误C2888:'auto :: operator()(_ T1)const':符号无法在命名空间'test'中定义
错误C2888:'auto ::(_ T1)':无法在命名空间'test'中定义符号
错误C2888: '::操作者未知类型(__cdecl*)(_ T1)(__ CDECL*(无效)noexcept常数)(_ T1)':符号不能命名空间 '测试' 中定义
...
对于一些不同的调用约定,后面跟着相同的错误.但是,没有周围命名空间的以下代码编译得很好.
template <class B>
struct A
{
static constexpr auto t = [](const auto c) {};
};
struct C : A<C>
{}; …Run Code Online (Sandbox Code Playgroud)