incomplete-type

Does T have to be a complete type to be used in `std::declval<T>`?

こ雲淡風輕ζ 提交于 2020-01-03 07:59:10
问题 Consider this example (coming from here): #include <type_traits> #include <iostream> template <typename U> struct A { }; struct B { template <typename F = int> A<F> f() { return A<F>{}; } using default_return_type = decltype(std::declval<B>().f()); }; int main() { B::default_return_type x{}; std::cout << std::is_same< B::default_return_type, A<int>>::value; } It compiles with no errors on gcc9.2 but gcc7.2 and clang 10.0.0 complain about B not being complete. Clangs error is: prog.cc:11:58:

Incomplete type for std::vector

廉价感情. 提交于 2020-01-01 04:34:06
问题 The GCC compiler complains (see below) when I try the following. class Face needs to be incomplete because it contains pointer to class Element which similarly contains pointer to class Face . In other words, there is a circular dependency among classes. How can I fix it? error: invalid application of ‘sizeof’ to incomplete type ‘Face’ class Face; // needs to be incomplete class Element { std::vector < std::unique_ptr <Face> > face; }; class Face { std::vector < std::unique_ptr <Element> >

static assert that template typename T is NOT complete?

試著忘記壹切 提交于 2019-12-30 03:14:05
问题 Is there a way to static_assert that a type T is Not complete at that point in a header? The idea is to have a compile error if someone adds #includes down the road in places they should not be. related: How to write `is_complete` template? Using that link's answer, namespace { template<class T, int discriminator> struct is_complete { static T & getT(); static char (& pass(T))[2]; static char pass(...); static const bool value = sizeof(pass(getT()))==2; }; } #define IS_COMPLETE(X) is_complete

Equivalence of p[0] and *p for incomplete array types

霸气de小男生 提交于 2019-12-29 04:35:08
问题 Consider the following code (it came about as a result of this discussion): #include <stdio.h> void foo(int (*p)[]) { // Argument has incomplete array type printf("%d\n", (*p)[1]); printf("%d\n", p[0][1]); // Line 5 } int main(void) { int a[] = { 5, 6, 7 }; foo(&a); // Line 10 } GCC 4.3.4 complains with the error message: prog.c: In function ‘foo’: prog.c:5: error: invalid use of array with unspecified bounds Same error message in GCC 4.1.2, and seems to be invariant of -std=c99 , -Wall ,

Handles Comparison: empty classes vs. undefined classes vs. void*

别等时光非礼了梦想. 提交于 2019-12-28 06:58:50
问题 Microsoft's GDI+ defines many empty classes to be treated as handles internally. For example, (source GdiPlusGpStubs.h ) //Approach 1 class GpGraphics {}; class GpBrush {}; class GpTexture : public GpBrush {}; class GpSolidFill : public GpBrush {}; class GpLineGradient : public GpBrush {}; class GpPathGradient : public GpBrush {}; class GpHatch : public GpBrush {}; class GpPen {}; class GpCustomLineCap {}; There are other two ways to define handles. They're, //Approach 2 class BOOK; //no need

Incomplete type, but I'm using forward declarations?

前提是你 提交于 2019-12-24 06:43:14
问题 What I'm doing: I have two files: game.h and sound_controller.h. Both of them are having conflicts with each other. game.h requires having a SoundController, while sound_controller.h requires having a Game, so they're both including each other's header files. The problem: game.h line 26: error: field soundController has incomplete type 'SoundController' I've included sound_controller.h in game.h, but it's saying the type is incomplete yet I've declared the class SoundController. So how do I

Prevent instantiation of template class with an incomplete type

强颜欢笑 提交于 2019-12-23 19:34:14
问题 I'm writing a library. Its layout looks something akin to this: ///////// // A.h // ///////// #include <vector> class B; class A { std::vector<B> Bs; public: ... }; ///////// // B.h // ///////// class B { ... } /////////// // A.cpp // /////////// #include "A.h" #include "B.h" // Implementation of A follows ... /////////// // B.cpp // /////////// #include "B.h" // Implementation of B follows ... ///////////// // MyLib.h // ///////////// #include "A.h" As you can see, the only type accessible

Why an inline declaration is not an incomplete type?

烈酒焚心 提交于 2019-12-23 06:57:33
问题 Consider the code below: struct Foo { struct Bar; Foo() { Bar bar; // Why isn't Bar an incomplete type?! } struct Bar {}; // Full definition }; // struct Bar {}; // fails to compile due to incomplete type int main() { Foo foo; } It compiles fine under at least 2 compilers (gcc5.2, clang3.5). My question is: Why isn't Bar considered an incomplete type in the constructor Foo::Foo , as I forward-declare it above the constructor but fully use it inside the constructor? Whenever I move Foo::Bar

Why are we allowed to take the address of an incomplete type?

这一生的挚爱 提交于 2019-12-22 04:04:30
问题 Consider this code: class Addressable; class Class1 { void foo(Addressable &a) { (void) &a; } }; // OK class Addressable { void *operator &() { return this; } }; class Class2 { void foo(Addressable &a) { (void) &a; } }; // Error: operator & private Why does C++ allow taking the address of an incomplete reference type? Couldn't it be potentially illegal, as shown above? Is this intentional? 回答1: Yes, that's intentional, and the possibility of breakage if operator& is overloaded is known.

Can't a class have static constexpr member instances of itself?

孤者浪人 提交于 2019-12-22 01:23:10
问题 This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result? struct Size { const unsigned int width; const unsigned int height; static constexpr Size big = { 480, 240 }; static constexpr Size small = { 210, 170 }; private: Size( ) = default; }; 回答1: Is there a way to achieve the same result? By "the same result", do you specifically intend the constexpr -ness of Size::big and