c++11

How to generate nested loops at compile time

亡梦爱人 提交于 2021-02-17 09:14:37
问题 I have an integer N which I know at compile time. I also have an std::array holding integers describing the shape of an N -dimensional array. I want to generate nested loops, as described bellow, at compile time, using metaprogramming techniques. constexpr int N {4}; constexpr std::array<int, N> shape {{1,3,5,2}}; auto f = [/* accept object which uses coords */] (auto... coords) { // do sth with coords }; // This is what I want to generate. for(int i = 0; i < shape[0]; i++) { for(int j = 0; j

“Use of undefined type” with unique_ptr to forward declared class and defaulted move constructor/assignment

折月煮酒 提交于 2021-02-17 05:46:22
问题 In the following code, is the only way to avoid a compile error and to include B.h implementing the move constructor / assignment manually in A.cpp? // A.h #include <memory> class B; // implementation somewhere in B.h/B.cpp class A { public: A() = default; ~A() = default; A(const A&) = delete; A& operator=(const A&) = delete; A(A&&) = default; A& operator=(A&&) = default; private: std::unique_ptr<B> m_b; }; Visual Studio 2015 gives "error C2027: use of undefined type", since the move

“Use of undefined type” with unique_ptr to forward declared class and defaulted move constructor/assignment

半腔热情 提交于 2021-02-17 05:45:24
问题 In the following code, is the only way to avoid a compile error and to include B.h implementing the move constructor / assignment manually in A.cpp? // A.h #include <memory> class B; // implementation somewhere in B.h/B.cpp class A { public: A() = default; ~A() = default; A(const A&) = delete; A& operator=(const A&) = delete; A(A&&) = default; A& operator=(A&&) = default; private: std::unique_ptr<B> m_b; }; Visual Studio 2015 gives "error C2027: use of undefined type", since the move

What is the default move construct?

老子叫甜甜 提交于 2021-02-17 03:30:36
问题 What is the definition of the default move constructor? I can't think of anything sensible. Maybe a swap on ptr members and copy on values/reference member? 回答1: That's what the standard says (12.8/15): The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [ Note: brace-or-equal-initializers of non-static data members are ignored. See also the example in 12.6.2. —end note ] The order of initialization is the same as the

Dynamically change text qlabel

血红的双手。 提交于 2021-02-17 03:16:43
问题 Sorry for my english. I need to change the text qlabel dynamically. class Game: { ... std::shared_ptr<QWidget> m_hint; QLabel *m_label; QHBoxLayout *m_layout; } void Game::setTextToHint(std::string str) { m_label = new QLabel(); m_layout = new QHBoxLayout(); m_label->setText(QString::fromUtf8(str.c_str())); m_layout->addWidget(m_label); m_hint->setLayout(m_layout); } And i use this function eg twice: setTextToHint("One"); setTextToHint("First"); But ultimately label = "One" Ok i understood. I

Dynamically change text qlabel

心不动则不痛 提交于 2021-02-17 03:15:02
问题 Sorry for my english. I need to change the text qlabel dynamically. class Game: { ... std::shared_ptr<QWidget> m_hint; QLabel *m_label; QHBoxLayout *m_layout; } void Game::setTextToHint(std::string str) { m_label = new QLabel(); m_layout = new QHBoxLayout(); m_label->setText(QString::fromUtf8(str.c_str())); m_layout->addWidget(m_label); m_hint->setLayout(m_layout); } And i use this function eg twice: setTextToHint("One"); setTextToHint("First"); But ultimately label = "One" Ok i understood. I

Variadic template resolution in VS2013 - Error C3520

孤街浪徒 提交于 2021-02-17 00:07:24
问题 What's wrong with this code? enum LogLevel { LogLevel_Error = 1, LogLevel_Warning = 2, LogLevel_Info = 3, LogLevel_Debug = 4 }; LogLevel GetLogLevel() {return LogLevel_Debug;}; void Write(const std::string& message) {}; void Write(LogLevel level, std::stringstream& ss) { if (level > GetLogLevel()) return; Write(ss.str()); } template<typename Arg> void Write(LogLevel level, std::stringstream& ss, Arg arg) { if (level > GetLogLevel()) return; ss << arg; Write(ss.str()); } template<typename

Variadic template resolution in VS2013 - Error C3520

淺唱寂寞╮ 提交于 2021-02-17 00:06:30
问题 What's wrong with this code? enum LogLevel { LogLevel_Error = 1, LogLevel_Warning = 2, LogLevel_Info = 3, LogLevel_Debug = 4 }; LogLevel GetLogLevel() {return LogLevel_Debug;}; void Write(const std::string& message) {}; void Write(LogLevel level, std::stringstream& ss) { if (level > GetLogLevel()) return; Write(ss.str()); } template<typename Arg> void Write(LogLevel level, std::stringstream& ss, Arg arg) { if (level > GetLogLevel()) return; ss << arg; Write(ss.str()); } template<typename

Perfect forwarding for functions inside of a templated C++ class

穿精又带淫゛_ 提交于 2021-02-16 20:32:47
问题 Is there a good way to get perfect forwarding for functions inside of a templated class? Specifically, in the code #include <iostream> // Forward declare a Bar struct Bar; // Two different functions that vary based on the kind of argument void printme(Bar const & bar) { std::cout << "printme: constant reference bar" << std::endl; } void printme(Bar && bar) { std::cout << "printme: r-value reference bar" << std::endl; } void printme2(Bar const & bar) { std::cout << "printme2: constant

no matching constructor for initialization of 'string' (aka 'basic_string<char>')

浪尽此生 提交于 2021-02-16 20:31:18
问题 Here is the code: #include <iostream> #include <string> using namespace std; class Foo { public: operator string() const { return n; } string n {"foo"}; }; int main (int argc, char** argv) { string s {Foo{}}; cout << s << endl; return 0; } This code compiles using gcc 4.8.3, but it does not compile using clang 3.5, can someone tell me what's wrong with it? I got an error like this: main.cpp:45:12: error: no matching constructor for initialization of 'string' (aka 'basic_string<char>') string