我正在尝试从std :: optional <>中的struct成员获取类型,这是成员函数的返回类型.
这是一个简化的例子:
struct Result
{
int tag;
int pos;
};
class Dict
{
public:
std::optional<Result> search(const char *word)
{
return Result{ 1,2 };
}
};
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
int main()
{
Dict abc;
decltype(abc.search(const char*)->pos) position;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 给定两个constexpr功能,是否可以将它们组合为一个功能?
template <char... C>
constexpr int boo()
{
char ch[] = { C... };
int count = 0;
for (char c : ch)
{
if (c != '0') count += 1;
}
return count;
}
template <char... C>
constexpr auto foo()
{
std::array<char, boo<C...>()> x{};
return x;
}
Run Code Online (Sandbox Code Playgroud)
如示例所示,我可以将' count'作为常量返回。我的问题是我不能count在声明的函数中使用' '作为常量。也就是说,如果将' boo()' 的主体放在' foo()'中,则编译器将抛出' count'不是常量。
c++ template-meta-programming variadic-templates constexpr c++17