简体   繁体   English

decltype的参数不正确

[英]Incorrect argument to decltype

I asked two days ago about creating threads to run non-static class methods with the Win32 API, and I almost got a solution but there is something confusing me, so I'm asking this question before posting the answer in my previous question. 我两天前问过有关使用Win32 API创建运行非静态类方法的线程,我几乎得到了一个解决方案但是有些让我感到困惑的事情,所以在我之前的问题中发布答案之前我问这个问题。

I'm trying to use this code to thread a function with an unknown return type: 我正在尝试使用此代码来线程化具有未知返回类型的函数:

template <class R, R func() >
unsigned int usualfunc() {
   func();
   return 1;
}

template <class R>
int Start(R(*func)()) {
   typedef decltype(&usualfunc<int, func>) D; // I get the error here , I can't get the address of the template function directly I need this
   D p = &usualfunc<R, func>;
   uintptr_t add = (uintptr_t)p;
   CreateThread(0, 0, (LPTHREAD_START_ROUTINE)add, 0, 0, 0);
   func();
   return 1;
}

int main() {
   Start(ltest);
}

When I try to compile the above code, I get : 当我尝试编译上面的代码时,我得到:

error 3556 'usualfunc': incorrect argument to 'decltype' 错误3556'ordinaryfunc':'decltype'的参数不正确

The error description is described on MSDN: MSDN上描述了错误描述:

Compiler Error C3556 编译器错误C3556

However, I tried another code before this, and it works just fine, but I wasn't very good with the syntax: 但是,在此之前我尝试了另一个代码,它工作得很好,但我的语法不是很好:

template <class R, R func() >
unsigned int usualfunc() {
   func();
   return 1;
}

 template <class R,R func()>
int Start() {
   typedef decltype(&usualfunc<int, func>) D; // works well
   D p = &usualfunc<R, func>;
   uintptr_t add = (uintptr_t)p;
   CreateThread(0, 0, (LPTHREAD_START_ROUTINE)add, 0, 0, 0);
   func();
   return 1;
}

int main() {
   Start<int,ltest>(); // works
}

I know this code is enough, but I'd like to use Start(ltest) instead of Start<int,ltest>() . 我知道这段代码已经足够了,但我想使用Start(ltest)而不是Start<int,ltest>()

Note: no one say that I should use the function as parameter in usualfunction , I'm using it as a template parameter because CreateThread() can't pass a function as a parameter. 注意:没有人说我应该在usualfunction使用函数作为参数,我将它用作模板参数,因为CreateThread()不能将函数作为参数传递。

Template parameters must be known at compile-time. 必须在编译时知道模板参数。 However you attempt to use the normal function parameter func as a template argument. 但是,您尝试使用普通函数参数func作为模板参数。

In the second code you give a template parameter as the template argument, which is fine. 在第二个代码中,您将模板参数作为模板参数,这很好。

Your first code is wrong for a similar reason as this code: 您的第一个代码是错误的,原因与此代码类似:

template<int X> void f() { }

int main(int argc, char **argv) { f<argc>(); }

although the error message is a bit more obscure. 虽然错误信息有点模糊。


Since C++17 you can get the syntax you want by making this modification to your second code: 从C ++ 17开始,您可以通过对第二个代码进行修改来获得所需的语法:

template <auto func>
int Start() {
    using R = decltype(func());
    // proceed as before...

and call it as Start<ltest>(); 并将其称为Start<ltest>(); .

Prior to C++17 you could use a macro with your second code: 在C ++ 17之前,您可以使用第二个代码的宏:

#define START(func) Start<decltype(func()), func>

as I'm using visual studio 2015 and can't use auto templates I used the macros as MM said and ended with this 因为我正在使用visual studio 2015并且无法使用自动模板,所以我使用了宏作为MM说并以此结束

Thread t;
t.SetFunc(ltest);
t.SetMember(testt, testf, tt); // class type , member function , pointer to class , it's like this : testt::testf , I can use this outside of class
t.SetMember(testt, testf, this); // from a member of the class , this is a pointer to the class , as I use this inside the member I can also thread private members here

with clang-cl 5 or 6 in visual studio 2015 : 在2015年视觉工作室中使用clang-cl 5或6:

template <auto func>
int Start() {
   using R = decltype(func());
   typedef decltype(&usualfunc<R, func>) D;
   D p = &usualfunc<R, func>;
   uintptr_t add = (uintptr_t)p;
   CreateThread(0, 0, (LPTHREAD_START_ROUTINE)add, 0, 0, 0);
   return 1;
}

this compiled well 这编译得很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM