模板和提升的编译器错误

Compiler error with templates and boost

本文关键字:编译器 错误      更新时间:2023-10-16

我正在创建一个小的线程管理对象,用于在我的程序中以更通用的方式启动线程(目前(。它是基于模板的,我使用提升线程作为底层线程"引擎"。我打算将其用作跨平台包装器,以替换我移植到Linux的一些遗留代码。

我的代码的基本实现如下所示:

using mythread = boost::thread; 
class ThreadManager
{
public:
    ThreadManager();
    ~ThreadManager() {}
    template<class _Fn, class... _Args>
    mythread* CreateThreadPtr(unsigned int Priority, ::std::string& name, unsigned int stackSz, _Fn&& func, _Args&&... args)
    {
        //boost::thread::attributes attrs;
        //attrs.set_stack_size(1024);
        //boost::thread t{attrs, thread};
        mythread* T = new mythread(func, args...);
        void* hndl = (void*)T->native_handle();
        if (hndl)
        {
            #ifdef WIN32
            BOOL res = SetThreadPriority(hndl, Priority);
            if (!res)
            {
                //dwError = GetLastError();
            }
            #else
            //TODO
            #endif
            SetThreadName(T, name);
            return T;
        }
        return NULL;
    }
    void SetThreadName(THANDLE thread, ::std::string& threadName);
#ifdef WIN32
    void SetThreadName(THREAD_ID threadId, ::std::string& threadName);
#endif
#ifdef __linux__
    void SetThreadName(void* hThread, ::std::string& threadName);
#endif
    THREAD_ID GetThreadId();
};
ThreadManager& Man(void);

将像这样使用:

m_TxRxThread = Man().CreateThreadPtr(Priority, thread_name, 0,ThreadProcTxRx, this);

但是当我构建它时,我收到此错误

/usr/include/boost/bind/bind.hpp:253:35: error: invalid conversion from 'CEth*' to 'long unsigned int' [-fpermissive]

有了-fpermissive一切似乎都很好,但这对我来说毫无意义,我想知道我在这里做错了什么!

提前感谢!

--编辑--

ThreadProcTxRxCEth对象的方法,其签名为

void CEth::ThreadProcTxRx(ul32 lpParam)

ul32在哪里typedef unsigned long...

写下最后这句话,众所周知的灯泡亮了起来......当然,这需要ul32. I was focusing on ThreadProcTxRx when the error pointed to这一点。

井。。。感谢禁卫军要求澄清。如果你想把它放在一个答案中,我会为此归功于你。

-允许

将有关不符合代码的某些诊断从错误降级为警告。因此,使用 -fpermissive 将允许编译一些不符合标准的代码。

通过使用-fpermissive进行编译,您将代码中的一些错误降级为要编译的警告。您应该避免这样做。

您的代码的问题是您正在尝试分配具有CEth*类型的unsigned long。如果可能,您需要static_cast退货到unsigned long或检查您的退货类型是否正确。