我想问一下建筑模式。我编写了两个片段代码来演示我的要求。
第一种方法是:
//a method on controller layer (in Spring framework)
@RequestMapping(...)
public ShopDTO findShop(final Long shopId) {
Shop shop = shopService.getShopById(shopId);
ShopDTO shopDTO = shopMapper.toShopDTO(shop);
return shopDTO;
}
//A method on service layer
@Transactional
public Shop getShopById(final Long shopId) {
//some code to find an entity by id
}
Run Code Online (Sandbox Code Playgroud)
ShopDTO注意:从商店实体映射到控制器层的代码。第二种方式是:
//a method on controller layer (in Spring framework)
@RequestMapping(...)
public ShopDTO findShop(final Long shopId) {
ShopDTO shopDTO = shopService.getShopById(shopId);
return shopDTO;
}
//A method on service layer
@Transactional …Run Code Online (Sandbox Code Playgroud) C++20 添加了概念,标准库中包含了相当多的概念。有一个概念特别引起了我的注意:std::invocable,它验证可以使用一组参数来调用函子。
std::invocable只是 的语法糖std::is_invocable。然而,标准库进一步定义了std::is_invocable_r哪些测试函子是否可以使用一组参数调用,而且一旦调用它就返回特定类型。nothrow这两个实用程序也有不同的版本。然而,标准中没有定义等效的概念。
该标准没有定义这些概念是否有原因,或者只是一个疏忽?是否有一些普通读者没有注意到的细节导致委员会决定不包括这些内容?
假设我想std::formatter绕过直接使用 来格式化单个对象std::format。我怎么做?
根据Formatter,我需要调用.format(value, format_ctx),其中format_ctx是std::format_contextor std::basic_format_context<...>。但我如何构建这个上下文呢?
该标准似乎没有提供一种构建方法。查看libstdc++ 源代码, 的成员变量basic_format_context都是私有的,没有非默认构造函数,并且如果不是friend.
这是否意味着std::formatter不可能按设计手动使用?
我为什么要这样做?
我想使用“调试格式”( "{?:}") 来格式化一个值(如果支持的话),回退到常规"{}".
检查支持的方法似乎是requires(std::formatter<T> f){f.set_debug_format();},我想如果我已经直接与格式化程序交互,我不妨只使用格式化程序本身。
我有一个简单的模板类A。如果满足某些要求,我想启用一项功能。
requires子句我尝试的第一个解决方案如下:
template <class T>
class A
{
public:
void a(void) requires (same_as<T, int>)
{
std::cout << "a" << std::endl;
};
};
Run Code Online (Sandbox Code Playgroud)
这效果非常好。我可以打电话,A<int>().a()但不能A<char>().a();。此外,IntelliSense 可以正确识别 Visual Studio 中的使用错误。
我尝试将函数定义移到类之外,但在 Visual Studio 中出现 C2511 编译器错误。在海湾合作委员会,它工作得很好。
template <class T>
class A
{
public:
void a(void) requires (same_as<T, int>);
};
template <class T>
void A<T>::a(void)
requires (same_as<T, int>)
{
std::cout << "a" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
您认为我的代码不正确还是 Visual Studio 编译器错误/功能不完整?
static_assert该解决方案在某些情况下有效,但如果您尝试显式模板实例化(例如template …
这是导致我出现一些问题的代码,尝试构建并获取错误:
“unary_function 基类未定义”和“unary_function”不是 std 的成员
std::unary_function 已在 C++17 中删除,那么什么是等效版本?
#include <functional>
struct path_sep_comp: public std::unary_function<tchar, bool>
{
path_sep_comp () {}
bool
operator () (tchar ch) const
{
#if defined (_WIN32)
return ch == LOG4CPLUS_TEXT ('\\') || ch == LOG4CPLUS_TEXT ('/');
#else
return ch == LOG4CPLUS_TEXT ('/');
#endif
}
};
Run Code Online (Sandbox Code Playgroud) 在以下示例中,第二个f函数重载的需要表达式的类型为std::integral_constant<bool,true>,它可以隐式转换为bool:
#include <type_traits>
struct S {
static constexpr bool valid = true;
};
template<typename T>
int f() { return 1; }
template<typename T>
int f() requires( std::bool_constant< T::valid >() ) { return 2; }
int main() {
return f<S>();
}
Run Code Online (Sandbox Code Playgroud)
可以观察到,GCC 由于类型不精确而拒绝该程序bool,但 Clang 接受,但选择了另一个重载int f() { return 1; }。演示: https: //gcc.godbolt.org/z/nf65zrxoK
这里哪个编译器是正确的?
如果类头名称包含嵌套名称说明符,则该类说明符不应位于类范围内。 如果其类名是标识符,则类说明符应对应于嵌套名称说明符所引用的类、类模板或命名空间中可命名的一个或多个声明;它们都应具有相同的目标范围,并且类说明符的目标范围的目标范围就是该范围。
[例子:
Run Code Online (Sandbox Code Playgroud)namespace N { template<class> struct A { struct B; }; } using N::A; template<class T> struct A<T>::B {}; // OK template<> struct A<void> {}; // OK--结束示例]
第一句话和例子似乎有所不同。
不包含嵌套名称说明符(但仅“依赖”使用A<void>)的模板的专业化是否仍然符合?
clang 和 msvc 接受它,gcc 显示错误
错误:在其命名空间之外显式特化“模板结构 N::A”必须使用嵌套名称说明符 [-fpermissive]
介绍原始措辞和示例注释的论文是P1787R6:声明以及在哪里找到它们,包含以下行:
Run Code Online (Sandbox Code Playgroud)template<> struct A<void> {}; // error: A not nominable in …
在以下 C++20 代码中,将 a 传递std::vector给带有参数的模板化函数std::span<T>会失败,因为显然编译器无法推导出模板参数。我已经用 GCC、Clang 和 MSVC 尝试过了;全部失败。
像这样调用有效:f3(std::span(vi))或f3(std::span(vp))。
我想知道为什么会失败,因为在我的理解中,std::vector是一个范围,并且std::span有范围的推导指南。
#include <memory>
#include <vector>
#include <span>
void f1(std::span<int> s)
{
}
void f2(std::span<std::shared_ptr<int>> s)
{
}
template<typename T>
void f3(std::span<T> s)
{
}
int main(int argc, char* argv[])
{
std::vector<int> vi;
std::vector<std::shared_ptr<int>> vp;
f1(vi);
f2(vp);
f3(vi); // ERROR: no matching function for call to 'f3'
f3(vp); // ERROR: no matching function for call to …Run Code Online (Sandbox Code Playgroud) 大约一两年前,我读到了 C++ 中的 SFINAE 规则。他们特别指出,
以下类型错误是 SFINAE 错误:
...
尝试创建 void 数组、引用数组、函数数组、负大小数组、非整数大小数组或大小为零的数组
我决定在我的作业中使用这个规则,但是它不起作用。逐渐减少它,我得到了这个我不理解的小代码示例:
#include <iostream>
template<int I>
struct Char {};
template<int I>
using Failer = Char<I>[0];
template<int I>
void y(Failer<I> = 0) {
std::cout << "y<" << I << ">, Failer version\n";
}
template<int I>
void y(int = 0) {
std::cout << "y<" << I << ">, int version\n";
}
int main() {
y<0>();
y<1>();
y<2>();
y<3>();
}
Run Code Online (Sandbox Code Playgroud)
而且,一些C++编译器似乎也不理解它。我创建了一个Godbolt 示例,您可以在其中找到三种不同的编译器以不同的方式解决y歧义:
int版本(这是我认为符合 …是否可以在 YAML 文件的根部分中有一个列表?到目前为止,我还没有看到遵循这种结构的文件,我一直在问自己它是否违反了语法。
下面是一个例子:
- 'entry A'
- 'entry B'
- 'entry C'
Run Code Online (Sandbox Code Playgroud)
到目前为止我所看到的:
list:
- 'entry A'
- 'entry B'
- 'entry C'
Run Code Online (Sandbox Code Playgroud)
换句话说,该list:部分是否已过时?