Libtorch compile error when defining D_GLIBCXX_DEBUG
Labels: module: build, module: abi, triaged

### 🐛 Describe the bug

I'm not sure if this is expected behaviour, but by having the compiler flag `-D_GLIBCXX_DEBUG` will result in a `use of deleted function` error. 

Minimal reproducible example here:

main.cpp:
```c++
#include <torch/torch.h>
int main() {
    return 0;
}
```

CMakeLists.txt:
```
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(main main.cpp)
target_link_libraries(main "${TORCH_LIBRARIES}")
set_property(TARGET main PROPERTY CXX_STANDARD 14)
target_compile_options(main PUBLIC
    -Wall -Wextra -D_GLIBCXX_DEBUG
)
```

cmake generated output:
```
$cmake -DCMAKE_PREFIX_PATH=/usr/local/libtorch ..
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found CUDA: /usr/local/cuda (found version "11.8")
-- The CUDA compiler identification is NVIDIA 11.8.89
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Caffe2: CUDA detected: 11.8
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 11.8
-- /usr/local/cuda/lib64/libnvrtc.so shorthash is 672ee683
-- USE_CUDNN is set to 0. Compiling without cuDNN support
-- Autodetected CUDA architecture(s):  8.6
-- Added CUDA NVCC flags for: -gencode;arch=compute_86,code=sm_86
-- Found Torch: /usr/local/libtorch/lib/libtorch.so
-- Configuring done (3.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/tuero/Documents/test/test_torch/build
```

Compiler error:
```
%$ make
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
In file included from /usr/local/libtorch/include/torch/csrc/autograd/variable.h:11,
                 from /usr/local/libtorch/include/torch/csrc/autograd/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/all.h:7,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
                 from /home/tuero/Documents/test/test_torch/main.cpp:1:
/usr/local/libtorch/include/ATen/NamedTensorUtils.h: In function ‘bool at::has_names(at::ITensorListRef)’:
/usr/local/libtorch/include/ATen/NamedTensorUtils.h:15:35: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
   15 |   return std::any_of(tensors.begin(), tensors.end(), [](const Tensor& t) {
      |                      ~~~~~~~~~~~~~^~
In file included from /usr/local/libtorch/include/ATen/WrapDimUtils.h:3,
                 from /usr/local/libtorch/include/ATen/TensorNames.h:3,
                 from /usr/local/libtorch/include/ATen/NamedTensorUtils.h:3,
                 from /usr/local/libtorch/include/torch/csrc/autograd/variable.h:11,
                 from /usr/local/libtorch/include/torch/csrc/autograd/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/all.h:7,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
                 from /home/tuero/Documents/test/test_torch/main.cpp:1:
/usr/local/libtorch/include/ATen/core/IListRef.h:362:7: note: ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’ is implicitly deleted because the default definition would be ill-formed:
  362 | class IListRefIterator {
      |       ^~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:362:7: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::Payload::~Payload()’
/usr/local/libtorch/include/ATen/core/IListRef.h:482:9: note: ‘c10::IListRefIterator<at::Tensor>::Payload::~Payload()’ is implicitly deleted because the default definition would be ill-formed:
  482 |   union Payload {
      |         ^~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:483:25: error: union member ‘c10::IListRefIterator<at::Tensor>::Payload::boxed_iterator’ with non-trivial ‘c10::impl::ListIterator<T, Iterator>::~ListIterator() [with T = at::Tensor; Iterator = __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<c10::IValue*, std::__cxx1998::vector<c10::IValue, std::allocator<c10::IValue> > >, std::__debug::vector<c10::IValue>, std::random_access_iterator_tag>]’
  483 |     boxed_iterator_type boxed_iterator;
      |                         ^~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:485:32: error: union member ‘c10::IListRefIterator<at::Tensor>::Payload::materialized_iterator’ with non-trivial ‘__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<const std::reference_wrapper<const at::Tensor>*, std::__cxx1998::vector<std::reference_wrapper<const at::Tensor>, std::allocator<std::reference_wrapper<const at::Tensor> > > >, std::__debug::vector<std::reference_wrapper<const at::Tensor>, std::allocator<std::reference_wrapper<const at::Tensor> > >, std::random_access_iterator_tag>::~_Safe_iterator()’
  485 |     materialized_iterator_type materialized_iterator;
      |                                ^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/local/libtorch/include/torch/csrc/autograd/variable.h:11,
                 from /usr/local/libtorch/include/torch/csrc/autograd/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/all.h:7,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
                 from /home/tuero/Documents/test/test_torch/main.cpp:1:
/usr/local/libtorch/include/ATen/NamedTensorUtils.h:15:50: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
   15 |   return std::any_of(tensors.begin(), tensors.end(), [](const Tensor& t) {
      |                                       ~~~~~~~~~~~^~
In file included from /usr/local/libtorch/include/ATen/core/dispatch/OperatorEntry.h:12,
                 from /usr/local/libtorch/include/ATen/core/dispatch/Dispatcher.h:6,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/types.h:12,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:4,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/data.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/all.h:9,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
                 from /home/tuero/Documents/test/test_torch/main.cpp:1:
/usr/local/libtorch/include/ATen/core/dispatch/DispatchKeyExtractor.h: In member function ‘void c10::detail::MultiDispatchKeySet::operator()(at::ITensorListRef)’:
/usr/local/libtorch/include/ATen/core/dispatch/DispatchKeyExtractor.h:79:28: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
   79 |       for (const auto& x : xs) {
      |                            ^~
/usr/local/libtorch/include/ATen/core/dispatch/DispatchKeyExtractor.h:79:28: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
In file included from /usr/local/libtorch/include/ATen/WrapDimUtils.h:3,
                 from /usr/local/libtorch/include/ATen/TensorNames.h:3,
                 from /usr/local/libtorch/include/ATen/NamedTensorUtils.h:3,
                 from /usr/local/libtorch/include/torch/csrc/autograd/variable.h:11,
                 from /usr/local/libtorch/include/torch/csrc/autograd/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/autograd.h:3,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/all.h:7,
                 from /usr/local/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
                 from /home/tuero/Documents/test/test_torch/main.cpp:1:
/usr/local/libtorch/include/ATen/core/IListRef.h: In instantiation of ‘c10::IListRef<T>::iterator c10::IListRef<T>::begin() const [with T = at::Tensor; c10::IListRef<T>::iterator = c10::IListRefIterator<at::Tensor>]’:
/usr/local/libtorch/include/ATen/NamedTensorUtils.h:15:35:   required from here
/usr/local/libtorch/include/ATen/core/IListRef.h:561:5: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
  561 |     TORCH_ILISTREF_UNWRAP(tag_, { return this_.begin(); });
      |     ^~~~~~~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:561:5: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
  561 |     TORCH_ILISTREF_UNWRAP(tag_, { return this_.begin(); });
      |     ^~~~~~~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:561:5: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
  561 |     TORCH_ILISTREF_UNWRAP(tag_, { return this_.begin(); });
      |     ^~~~~~~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h: In instantiation of ‘c10::IListRef<T>::iterator c10::IListRef<T>::end() const [with T = at::Tensor; c10::IListRef<T>::iterator = c10::IListRefIterator<at::Tensor>]’:
/usr/local/libtorch/include/ATen/NamedTensorUtils.h:15:50:   required from here
/usr/local/libtorch/include/ATen/core/IListRef.h:565:5: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
  565 |     TORCH_ILISTREF_UNWRAP(tag_, { return this_.end(); });
      |     ^~~~~~~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:565:5: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
  565 |     TORCH_ILISTREF_UNWRAP(tag_, { return this_.end(); });
      |     ^~~~~~~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h:565:5: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::~IListRefIterator()’
  565 |     TORCH_ILISTREF_UNWRAP(tag_, { return this_.end(); });
      |     ^~~~~~~~~~~~~~~~~~~~~
/usr/local/libtorch/include/ATen/core/IListRef.h: In instantiation of ‘c10::IListRefIterator<T>::IListRefIterator(c10::IListRefIterator<T>::unboxed_iterator_type) [with T = at::Tensor; c10::IListRefIterator<T>::unboxed_iterator_type = const at::Tensor*]’:
/usr/local/libtorch/include/ATen/core/IListRef.h:561:5:   required from ‘c10::IListRef<T>::iterator c10::IListRef<T>::begin() const [with T = at::Tensor; c10::IListRef<T>::iterator = c10::IListRefIterator<at::Tensor>]’
/usr/local/libtorch/include/ATen/NamedTensorUtils.h:15:35:   required from here
/usr/local/libtorch/include/ATen/core/IListRef.h:433:78: error: use of deleted function ‘c10::IListRefIterator<at::Tensor>::Payload::~Payload()’
  433 |   IListRefIterator(unboxed_iterator_type unboxed) : tag_(IListRefTag::Unboxed) {
      |                                                                              ^
make[2]: *** [CMakeFiles/main.dir/build.make:76: CMakeFiles/main.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
```

Removing the `-D_GLIBCXX_DEBUG` works as expected.

### Versions

```
$ cat /usr/local/libtorch/build-version
2.0.1+cu118

$ g++ --version
g++ (Ubuntu 11.3.0-6ubuntu1) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

cc @malfet @seemethere