这是一个模板函数,它接受一个指针(或类似指针的对象)和一个成员函数:
template <typename Ptr, typename MemberFunctor>
int example(Ptr ptr, MemberFunctor func )
{
return (ptr->*func)();
}
Run Code Online (Sandbox Code Playgroud)
如果与普通指针一起使用时有效:
struct C
{
int getId() const { return 1; }
};
C* c = new C;
example(c, &C::getId); // Works fine
Run Code Online (Sandbox Code Playgroud)
但它不适用于智能指针:
std::shared_ptr<C> c2(new C);
example(c2, &C::getId);
Run Code Online (Sandbox Code Playgroud)
错误信息:
error: C2296: '->*' : illegal, left operand has type 'std::shared_ptr<C>'
Run Code Online (Sandbox Code Playgroud)
为什么?以及如何制作适合两者的东西?
关于状态字段和类似的预定义值集存在重复出现的问题.
让我们举一个订单系统的例子,订单实体的状态可以是New,In Progress,Paid等.
订单的状态需要
如何保持这三项活动:
以下是一些具有优缺点的示例实现:
订单表引用状态的ID.
CREATE TABLE `status` (
`id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`));
CREATE TABLE IF NOT EXISTS `order` (
`id` INT NOT NULL AUTOINCREMENT,
`status_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `order_status_idx` (`status` ASC),
CONSTRAINT `order_status_id`
FOREIGN KEY (`status_id`)
REFERENCES `status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Run Code Online (Sandbox Code Playgroud)后端代码有一个枚举,它为代码中的这些预定义整数赋予了意义
enum Status {
PAID = 7;
};
// While processing …Run Code Online (Sandbox Code Playgroud)我想使用Client Credentials来验证客户端应用程序以访问API.
我的问题是创建客户端凭据.使用php artisan passport:client要求我输入user_id以将客户端关联到该用户.我不明白.为什么客户端应用程序必须与用户关联?!或者还有另一种方式吗?
passport:client命令仅支持创建密码授予客户端和个人授权客户端.我不认为他们中的任何一个是我需要的.
我真正需要的是创建客户端凭据,仅客户端应用程序使用它来授权自己访问某些API.怎么做?
我相信这是一个简单的问题,可能没有简单的答案。
这是代码:
template<typename T>
T* copy(T* original, int size) {
T* result = new T[size];
// At this point the default constructor of all new T objects have been called.
for(int i = 0; i < size; ++i) {
// This will call the assignment operator= on all new T objects
result[i] = original[i];
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
题:
有没有办法使用T的复制构造函数而不是使用默认构造函数后跟赋值运算符来初始化新分配的内存?
目的是使用 T 的复制构造函数将每个元素复制到新数组中的类似元素。
我想有一种方法可以通过使用分配内存malloc,然后为每个元素调用复制构造函数来做到这一点,但我不知道如何做到这一点。
这是我想象中的示例解决方案。如果这是正确的,或者这是我们能得到的最好的结果,请告诉我。或者提出更好的解决方案:
template<typename T>
T* copy(T* original, int size) {
T* …Run Code Online (Sandbox Code Playgroud) 以下问题是C++ Test on Upwork的一部分.
Run Code Online (Sandbox Code Playgroud)class A { typedef int I; // private member I f(); friend I g(I); static I x; };以下哪项有效:
1)
A::I A::f() { return 0; }2)
A::I g(A::I p = A::x);3)
A::I g(A::I p) { return 0; }4)
A::I A::x = 0;
在我看来,所有答案都是有效的.我测试了它们,它们工作得很好!
我对吗?还是我错过了什么?
使用Visual C++ 2013,以下代码产生一个奇怪的编译错误:
/// header.h
class Test
{
public:
template <typename Func>
void operator[](Func f)
{
f();
}
};
template <typename T>
void funct()
{
Test t;
t[[](){; }]; // line 16 // The same error with t[ ([](){; }) ];
}
/// main.cpp
int main()
{
funct<int>();
// funct();
}
Run Code Online (Sandbox Code Playgroud)
错误:
1>c:\path\to\header.h(16): error C2958: the left bracket '[' found at 'e:\path\to\header.h(16)' was not matched correctly
1>c:\path\to\header.h(16): error C2059: syntax error : ']'
1>c:\path\to\header.h(17): error C2059: syntax error : '}' …Run Code Online (Sandbox Code Playgroud) 以下是所需 URL 的方案:
/service/getBalance should map to CustomerController::getBalance
/service/addBalance should map to CustomerController::addBalance
/customer/getBalance should map to CustomerController::getBalance
/customer/addBalance should map to CustomerController::addBalance
Run Code Online (Sandbox Code Playgroud)
这是一个简单的控制器
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class CustomerController extends Controller {
/**
* @Route("/getBalance")
*/
public function getBalanceAction(Request $request) {
}
/**
* @Route("/addBalance")
*/
public function addBalanceAction(Request $request) {
}
} // class CustomerController
Run Code Online (Sandbox Code Playgroud)
试过以下方法。他们都没有工作。
# rounting.yml
v1:
resource: "@AppBundle/Controller/CustomerController.php"
prefix: /service
type: annotation
v2:
resource: "@AppBundle/Controller/CustomerController.php"
prefix: /customer
type: annotation
Run Code Online (Sandbox Code Playgroud)
加载具有不同前缀的相同资源总是会覆盖上一次出现(最后一次有效)。以下也不会出于相同的原因并具有相同的行为。
# rounting.yml …Run Code Online (Sandbox Code Playgroud)