[修正] 「ジェネリックラムダ」と書いていたが、ジェネリックラムダは別のなにかだったのでタイトル等を変更
前提・実現したいこと
C++ で遊んでいます。
クラスの static かつ非型テンプレートなメンバにラムダ式を持ったときに以下のエラーが出ました。
テンプレートは実体化された型や値ごとに別の名前を与えられると思っていたのですが、
実は違うのでしょうか?
また、解決策としてはどのようなものがあるでしょうか?
※やりたいこと:
・非型テンプレートでラムダを定義したい
・それをあるクラスが static 変数としてもつようにしたい
発生している問題・エラーメッセージ
シンボルの重複が報告されます。
$ g++ -std=c++17 a.cpp C:\msys64\tmp\ccKmfXl2.s: Assembler messages: C:\msys64\tmp\ccKmfXl2.s:26: Error: symbol `_ZNK1AUlvE_clEv' is already defined
該当のソースコード
C++
1// a.cpp 2#include <iostream> 3 4struct A { 5 template <int N> 6 static constexpr inline auto X = []() { return N; }; 7 8 template <int N> 9 static constexpr inline int Y = N; 10}; 11 12int main() { 13 std::cout << A::X<1>() << std::endl; // ← ここをコメントアウトするとエラーは発生しなくなる 14 std::cout << A::X<2>() << std::endl; 15 std::cout << A::Y<1> << std::endl; 16 std::cout << A::Y<2> << std::endl; 17 return 0; 18}
試したこと
clang++ でコンパイルするとエラーにはなりませんでしたが、期待する出力にはなりませんでした。
$ clang++ -std=c++17 && ./a 1 1 1 2 // 期待する出力: // 1 // 2 // 1 // 2
補足情報(FW/ツールのバージョンなど)
$ g++ --version g++.exe (Rev2, Built by MSYS2 project) 7.3.0 Copyright (C) 2017 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. $ clang++ --version clang version 5.0.1 (tags/RELEASE_501/final) Target: x86_64-w64-windows-gnu Thread model: posix InstalledDir: C:\msys64\mingw64\bin
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/12 14:47
2018/05/12 15:10
2018/05/12 15:29
2018/05/15 04:01
2018/05/15 05:43
2018/05/15 06:11
2018/05/15 07:00