This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
Bug 1616 - Narrowing in variadic templates
Summary: Narrowing in variadic templates
Status: CONFIRMED
Alias: None
Product: Eigen
Classification: Unclassified
Component: Tensor (show other bugs)
Version: 3.4 (development)
Hardware: All All
: Normal Compilation Problem
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-10-21 11:22 UTC by Laurent Lemmens
Modified: 2019-12-04 18:04 UTC (History)
5 users (show)



Attachments

Description Laurent Lemmens 2018-10-21 11:22:59 UTC
When compiling code with Apple Clang that uses `operator()` of some Tensor, I am getting a compilation error "Non-constant-expression cannot be narrowed from type 'unsigned long' to 'std::__1::array<long, 4>::value_type' (aka 'long') in initializer list" on line 199 from Tensor.h.

I have managed to fix this on my machine by replacing line 199 by
```
199: return this->operator()(array<Index, Numindices>{{firstIndex, secondIndex, static_cast<long>(otherIndices)...}});
```
but I'm not sure if this is the most general way to go.
Comment 1 Christoph Hertzberg 2018-10-21 12:25:58 UTC
When compiling what code? 
With which clang version exactly? 
With what compilation options?

This seems to work with clang-3.5 or newer:
https://godbolt.org/z/0rOASs

If the static cast is needed, then it should cast to `Index` instead of `long`.
Comment 2 Laurent Lemmens 2018-10-21 16:20:02 UTC
I'm sorry for not providing adequate information.


I have tried to compile your toy code (and added a main() function to make it stand-alone compilable):
```cpp
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>


double foo(const Eigen::Tensor<double, 4> T)
{
    unsigned long j=3, k=4;
    return T(1,2,j,k);
}

int bar()
{
    return EIGEN_HAS_VARIADIC_TEMPLATES;
}


int main() {

    Eigen::Tensor<double, 4> g (1, 2, 3, 4);
    std::cout << foo(g) << std::endl;
    return bar();
}
```
and I am still getting the same compiler error:
In file included from /Users/laurentlemmens/Software/Sandbox/Sandbox/main.cpp:1:
In file included from /opt/local/include/eigen3/unsupported/Eigen/CXX11/Tensor:143:
/opt/local/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:199:82: error: non-constant-expression cannot be narrowed from type 'unsigned long' to 'std::__1::array<long, 4>::value_type' (aka 'long') in initializer list [-Wc++11-narrowing]
      return this->operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});


I am using Xcode Version 10.0 (10A255), which seems to be using Apple Clang 10.0.0 (clang-1000.11.45.2) (https://en.wikipedia.org/wiki/Xcode#Latest_versions).


I am using no special compiler flags, so the actual compilation command is given by (it's rather verbose):
CompileC /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Objects-normal/x86_64/main.o /Users/laurentlemmens/Software/Sandbox/Sandbox/main.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler (in target: Sandbox)
    cd /Users/laurentlemmens/Software/Sandbox
    export LANG=en_US.US-ASCII
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu++14 -stdlib=libc++ -fmodules -gmodules -fmodules-cache-path=/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -fno-common -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.14 -g -fvisibility-inlines-hidden -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -index-store-path /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Index/DataStore -iquote /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Sandbox-generated-files.hmap -I/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Sandbox-own-target-headers.hmap -I/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Sandbox-all-target-headers.hmap -iquote /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Sandbox-project-headers.hmap -I/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Products/Debug/include -I/opt/local/include/eigen3 -I/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/DerivedSources/x86_64 -I/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/DerivedSources -F/Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Products/Debug -MMD -MT dependencies -MF /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Objects-normal/x86_64/main.dia -c /Users/laurentlemmens/Software/Sandbox/Sandbox/main.cpp -o /Users/laurentlemmens/Library/Developer/Xcode/DerivedData/Sandbox-fjdujvfsuhbwmpgnctvprtikmgmt/Build/Intermediates.noindex/Sandbox.build/Debug/Sandbox.build/Objects-normal/x86_64/main.o


I have furthermore replaced line 199 by
```cpp
return this->operator()(array<Index, Numindices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
```
and the compilation error is then resolved.
Comment 3 Laurent Lemmens 2018-10-21 16:22:23 UTC
One additional detail: I am using MacPort's Eigen3 (eigen3 @3.3.5).
Comment 4 Gael Guennebaud 2018-10-21 20:08:26 UTC
I can reproduce with any clang version (with -std=c++11), using either the 3.3 or default branch.
Comment 5 Christoph Hertzberg 2018-10-22 12:15:33 UTC
I was just too lazy to test it locally, and godbolt appears to include Eigen via -isystem and not -I, therefore masking the error.

The only minor issue I see with the proposed patch is that it would hide accidentally passing non-integer values to operator(Index, Index, ...), i.e., the following would compile without warnings:

    T(1,2,3.1,4.2);
Comment 6 Nobody 2019-12-04 18:04:17 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/1616.

Note You need to log in before you can comment on or make changes to this bug.