相关疑难解决方法(0)

为什么我不能在另一个函数中定义一个函数?

这不是lambda函数问题,我知道我可以将lambda赋给变量.

允许我们声明,但不在代码中定义函数是什么意思?

例如:

#include <iostream>

int main()
{
    // This is illegal
    // int one(int bar) { return 13 + bar; }

    // This is legal, but why would I want this?
    int two(int bar);

    // This gets the job done but man it's complicated
    class three{
        int m_iBar;
    public:
        three(int bar):m_iBar(13 + bar){}
        operator int(){return m_iBar;}
    }; 

    std::cout << three(42) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道的是为什么C++会允许two哪些看似无用,three哪个看起来更复杂,但却不允许one

编辑:

从答案中可以看出,代码内声明可能能够防止命名空间污染,我希望听到的是为什么声明函数的能力已被允许,但是不允许定义函数的能力.

c++ functor function-declaration

78
推荐指数
5
解决办法
3万
查看次数

花括号内的c ++ {*this}

以下代码编译正常:

g++ -std=c++11 test.cpp -Wall -Wextra -Wfatal-errors && ./a.out
Run Code Online (Sandbox Code Playgroud)

但是,如果我从中移除花括号{*this}并使用*this,我将面临错误:

错误:使用已删除的函数'Obj :: Position :: Position(Obj :: Position &&)'

{*this}和之间有什么区别*this

class Obj
{
    template<bool> friend class Position;

    double data;
public:
    class Position
    {
        const Obj& ref;
    public:
        inline Position(const Obj& ref): ref(ref){}
        inline Position(Position const &) = delete;
        inline Position(Position &&) = delete;
    };
    inline Obj(){}
    inline Obj(const double &data): data(data){}
    inline auto get_pos() const-> Position{return {*this};} /* <--- here */
    inline auto get_pos()-> …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

50
推荐指数
3
解决办法
3174
查看次数

不允许嵌套函数,但为什么允许嵌套函数原型?[C++]

我正在阅读相关问题,这个问题引导我提出这个问题.

请考虑以下代码

int main()
{
    string SomeString();
}
Run Code Online (Sandbox Code Playgroud)

总而言之,编译器将其作为函数原型而不是字符串对象.现在考虑以下代码.

int main()
{
    string Some()
    {
        return "";
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器说这是无效的,因为我猜不允许嵌套函数定义.如果不允许,为什么允许嵌套函数原型?它没有给予任何优势而不是混淆(或者我在这里错过了一些有效点?).

我想出以下是有效的.

int main()
{ 
  string SomeFun();
  SomeFun();
  return 0;
}

string SomeFun()
{
  std::cout << "WOW this is unexpected" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这也令人困惑.我期待函数SomeFun()只在main中有一个范围.但是我错了.为什么编译器允许编译如上所述的代码?有没有像上面这样的代码有意义的实时情况?

有什么想法吗?

c++ compiler-construction nested function

7
推荐指数
4
解决办法
3559
查看次数

更简单的方法是为矢量(或者STL中的其他东西)做回调吗?C++

我正在制作一个简单的犯罪模拟游戏.

在整个过程中,我一遍又一遍地做同样的事情:

// vector<Drug*> drugSack;
for (unsigned int i = 0; i < this->drugSack.size(); i++)
            this->sell(drugSack[i]);
Run Code Online (Sandbox Code Playgroud)

只是一个例子.我讨厌所有这些for循环遍布整个地方omg QQ,无论如何做类似的事情:

drugSack->DoForAll((void*)myCallBack);
Run Code Online (Sandbox Code Playgroud)

我不太熟悉STL.

c++ stl vector

4
推荐指数
2
解决办法
649
查看次数

在 std::map 中使用 std::less 无法编译

我无法理解为什么以下函数不能编译

#include <iostream>
#include <map>

int main(){
  std::map<int, int, std::less<int>> myMap(std::less<int>());
  myMap[2] = 2;
  std::cout << myMap[2] << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误信息如下——

std_less_check.cpp: In function ‘int main()’:
std_less_check.cpp:6:10: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   myMap[2] = 2;
          ^
std_less_check.cpp:6:14: error: assignment of read-only location ‘*(myMap + 2)’
   myMap[2] = 2;
              ^
std_less_check.cpp:6:14: error: cannot convert ‘int’ to ‘std::map<int, int, std::less<int> >(std::less<int> (*)())’ in assignment
std_less_check.cpp:7:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ constructor most-vexing-parse c++11

4
推荐指数
1
解决办法
96
查看次数

boost disable_interruption不会阻止thread_interrupted

我试图防止线程在特定范围内时中断.但是,使用boost::this_thread::disable_interruption di()似乎没有任何影响.

#include <boost/thread.hpp>
#include <iostream>

void worker() {
    std::cout << "START" << std::endl;

    for(;;) {
        {
            boost::this_thread::disable_interruption di();
            try {
                boost::this_thread::sleep(boost::posix_time::seconds(1));
            }
            catch(boost::thread_interrupted & e) {
                assert( false );
            }
        }

        try {
            boost::this_thread::interruption_point();
        }
        catch(boost::thread_interrupted & e) {
            break;
        }

    }

    std::cout << "END" << std::endl;
}


int main() {
    boost::thread thread(&worker);
    thread.interrupt();
    thread.join();
}
Run Code Online (Sandbox Code Playgroud)

文档似乎暗示boost::this_thread::sleep()不会抛出boost::thread_interrupted,而di在范围内.

我究竟做错了什么?

c++ boost boost-thread

2
推荐指数
1
解决办法
951
查看次数